Laravel-Test-Helpers icon indicating copy to clipboard operation
Laravel-Test-Helpers copied to clipboard

Unable to generate attributes if the model has a non-standard table name

Open ingro opened this issue 11 years ago • 5 comments

I was trying to use this library and found a (possible?) bug.

For various reason I don't have standard table's name and so I used the $table property inside each model to connect it to the right table.

So when I tried to use: Factory::attributesFor('Pages'); I only got an empty array, and the same with the other available methods.

Further investigation pointed me to the parseTableName method on the Factory class:

protected function parseTableName($class)
{
        return $this->isNamespaced($class)
            ? str_plural(substr(strrchr($class, '\\'), 1))
            : str_plural($class);
}

I'm actually using a workaround, replacing the return value using the getTable method of Eloquent's model, but I don't know if this is the right way to fix it...

ingro avatar Jul 02 '13 14:07 ingro

I'm experiencing a similar issue to this (i.e. running Factory::attributesFor('User') returns nothing but an empty array) - although I am using the standard table names for the models.

LimeBlast avatar Jul 16 '13 15:07 LimeBlast

UPDATE: Having had a bit of a play myself, I've also found that the issue lies in the parseTableName() function, although my issue was fixed by throwing a strtolower() into the equation:

protected function parseTableName($class)
{
    return strtolower($this->isNamespaced($class)
        ? str_plural(substr(strrchr($class, '\\'), 1))
        : str_plural($class));
}

This, however, obviously doesn't fix @ingro's issue, which I can only imagine being fixed by getting the table name from the model itself - although I'm not sure quite how to go about doing such a thing.

LimeBlast avatar Jul 16 '13 15:07 LimeBlast

Pull request #27 should fix both of these issues :smile:.

Factory::attributesFor('Pages'); and Factory::attributesFor('pages'); will now use the pages table unless a public $table variable is declared in the model.

jamime avatar Jul 17 '13 15:07 jamime

Good stuff, looking forward to it being accepted into the repo :+1:

LimeBlast avatar Jul 17 '13 15:07 LimeBlast

I'm experiencing the same problem, too. I create some tables and models in Turkish and parseTableName does not work.

BTW I don't think getTable() is a good solution here. What if the model isn't extending Eloquent? We can't use getTable() then.

ekrembk avatar Aug 11 '13 00:08 ekrembk