Load model from schema (Cake/2.x)

I’m writing a plugin using Cake/2.5.5 and I want to insert sample data. For that, I’m writing a method in my custom FooManagerSchema class that extends CakeSchema, which I execute from shell:

Console\cake schema create --plugin FooManager --path /path/to/Config/Schema --file sample_data.php

I can successfully retrieve existing data this way:

/* @var $vehicleModel Vehicle */
$vehicleModel = ClassRegistry::init('Vehicle');
debug($vehicleModel->find('all'));

However, I can’t manage to obtain associated records:

/* @var $vehicleModel Vehicle */
$vehicleModel = ClassRegistry::init('Vehicle');
debug($vehicleModel->find('all', array(
    'contain' => array('Person')
)));

… triggers this:

Warning Error: Model “Vehicle” is not associated with model “Person”

… and results lack the Person arrays, even though a fairly similar code works fine in VehiclesController:

debug($this->Vehicle->find('all', array(
    'contain' => array('Person'),
)));

… what means that model associations are correct.

What am I missing?

Asking in a forum is often enough to find the answer yourself :yum:

I was missing the plug-in prefix:

$vehicleModel = ClassRegistry::init('Vehicle');

… should be:

$vehicleModel = ClassRegistry::init('FooManager.Vehicle');
1 Like