Hmmm, funny...
I could not use $this->Model-> ModelsPart->find() from
models_controller.php, yet, I could use $this->ModelsPart->find().
Oh well, I guess as long as it works :P
Thanks guys..
On May 21, 2008, at 5:02 PM, Reza Muhammad wrote:
> You know,
>
> I did whatever you said, but things still didn't work the way I
> hoped. Or maybe theres something I didn't get from you.
>
> 1. I have to have models/model.php and models/part.php and models/
> parts_model.php right?
> 2. I also need to setup a database called 'models', 'parts', and the
> HABTM table 'models_parts' ?
> 3. In my models/model.php and models/part.php, and in those files, I
> need to have something like this:
> model.php:
> var $hasAndBelongsToMany = array(
> 'Part' => array('className' => 'Part',
> 'joinTable' => 'models_parts',
> 'foreignKey' => 'model_id',
> 'associationForeignKey' => 'part_id',
> 'with' => 'ModelsPart',
> 'unique' => true,
> 'conditions' => '',
> 'fields' => '',
> 'order' => '',
> 'limit' => '',
> 'offset' => '',
> 'finderQuery' => '',
> 'deleteQuery' => '',
> 'insertQuery' => ''
> )
> );
> part.php:
> var $hasAndBelongsToMany = array(
> 'Model' => array('className' => 'Model',
> 'joinTable' => 'models_parts',
> 'foreignKey' => 'part_id',
> 'associationForeignKey' => 'model_id',
> 'with' => 'ModelsPart',
> 'unique' => true,
> 'conditions' => '',
> 'fields' => '',
> 'order' => '',
> 'limit' => '',
> 'offset' => '',
> 'finderQuery' => '',
> 'deleteQuery' => '',
> 'insertQuery' => ''
> )
> );
> models_part.php:
> var $belongsTo = array(
> 'Model' => array('className' => 'Model',
> 'foreignKey' =>
> 'model_id',
> 'conditions' =>
> '',
> 'fields' => '',
> 'order' => ''
> ),
> 'Part' => array('className' => 'Part',
> 'foreignKey' =>
> 'part_id',
> 'conditions' =>
> '',
> 'fields' => '',
> 'order' => ''
> )
> );
> 4. WIth the above setup, I should've been able to run?
> $this->Model-> ModelsPart->find('all',
> array('conditions'=>array('ModelsPart.model_id'=>$id)));
>
> However, all I got is the error saying the following:
>
> Notice (8): Undefined property: Model::$ModelsPart [APP/controllers/
> models_controller.php, line 33]
> Code | Context
> $id = "16"
> $this->Model->recursive = 2;
> $this->set('model', $this->Model->find($id));
> debug($this->Model->ModelsPart->find('all',
> ModelsController::view() - APP/controllers/models_controller.php,
> line 33
> Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 268
> Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 240
> [main] - APP/webroot/index.php, line 84
>
>
> Is there anything else I should do? This is really frustrating for
> me, since I can already get related models from parts_controller,
> but I cannot do the other way around.
>
> Thanks alot.
>
> On May 20, 2008, at 10:45 PM, Joel Perras wrote
>
>>
>> If I understand correctly, you want to be able to find parts that
>> belong to a common model from your Model controller.
>>
>> First, you should add in the 'with' parameter to your Parts model and
>> Model model (say that a couple times fast). In your PartsModel:
>> 'with'=> 'ModelsPart' and similarly in your ModelModel. This will
>> tell Cake to bind the join models correctly so that you can retrieve
>> associated model data through simple find() calls instead of having
>> to
>> dynamically bind and unbind models.
>>
>> Now you should be able to run:
>> $this->Model->PartsModel->find('all',
>> array('conditions'=>array('PartsModel.model_id'=>4))
>>
>> Also, if you live on the bleeding edge of the Cake nightlies, then
>> you
>> might want to check out the 'Containable' behaviour. It has zero
>> documentation as of this point, but the test cases give you a good
>> idea of what it's meant to do.
>>
>> -Joel.
>>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---