Skip to content

Latest commit

 

History

History
63 lines (52 loc) · 2.08 KB

no_cake_associations.md

File metadata and controls

63 lines (52 loc) · 2.08 KB

Associations for non-CakePHP apps

Associations can be defined within the factories in the initialize() method. The getTable() method provides public access to the model class used by the factories. If not defined in your application (which is probably the case if not built with CakePHP), the model class is generated automatically, based on the table name returned by the getRootTableRegistryName method.

For example, considering the following schema:

addresses cities countries
id id id
street name name
city_id country_id created
created created modified
modified modified

First create the CityFactory, AddressFactory and CountryFactory classes as described here.

In the CityFactory, you may then associate the cities belonging to a country and having many addresses in the initialize method:

use CakephpFixtureFactories\Factory\BaseFactory;
use Faker\Generator;

class CityFactory extends BaseFactory
{
    protected function initialize(): void
    {
        $this->getTable()
            ->belongsTo('Country')
            ->hasMany('Addresses');
    }
    
    protected function getRootTableRegistryName(): string
    {
        return "Cities";
    }   
    
    protected function setDefaultTemplate(): void
    {
        $this->setDefaultData(function (Generator $faker) {
            return [
                'name' => $faker->city(),
            ];
        })
        ->withCountry();
    }
}

Once this is defined, you may then call:

$city = CityFactory::make()
    ->with('Addresses', 4)
    ->with('Country', ['name' => 'India'])
    ->getEntity();

which will set the city's country, and provide 4 random addresses.

You will find described in the cookbook HERE how to define your associations. Non CakePHP applications will not need to create any table objects, but rather use the getTable() public method.