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.
On May 20, 5:05 am, Reza Muhammad <[EMAIL PROTECTED]> wrote:
> Thanks for the link.
>
> I did modelize my HABTM table, now I have a "ModelsPart" model, and it
> has a $belongsTo to Part and Model.
>
> Unfortunately, getting related parts when I'm using find() or
> findAll() in ModelsController doesnt work, but I can get related
> models from PartsController.
>
> Any more ideas?
>
> On May 20, 2008, at 3:27 PM, Matt Huggins wrote:
>
>
>
> > You need to modelize your HABTM table. Hopefully this link will help
> > you out.
>
> >http://cricava.com/blogs/index.php?blog=6&title=modelizing_habtm_join...
>
> > On May 20, 3:04 am, Reza Muhammad <[EMAIL PROTECTED]> wrote:
> >> Hi guys.
>
> >> I just tried to setup a HABTM relationship between "Model" and
> >> "Parts". In my "Model" model. I already created tables called
> >> 'models', 'parts', and 'models_parts'.
>
> >> Here's what I have in my models:
>
> >> - model.php
>
> >> class Model extends AppModel {
>
> >> var $name = 'Model';
> >> var $useTable = 'models';
> >> var $validate = array(
> >> 'name' => array(
> >> 'rule' => VALID_NOT_EMPTY,
> >> 'required' => true,
> >> 'message' => 'Model name must only contains
> >> letters and numbers'
> >> )
> >> );
> >> //The Associations below have been created with all
> >> possible keys,
> >> those that are not needed can be removed
> >> var $hasAndBelongsToMany = array(
> >> 'Part' => array('className' => 'Part',
> >> 'joinTable' => 'models_parts',
> >> 'foreignKey' => 'model_id',
> >> 'associationForeignKey' => 'part_id',
> >> 'unique' => true,
> >> 'conditions' => '',
> >> 'fields' => '',
> >> 'order' => '',
> >> 'limit' => '',
> >> 'offset' => '',
> >> 'finderQuery' => '',
> >> 'deleteQuery' => '',
> >> 'insertQuery' => ''
> >> )
> >> );
>
> >> }
>
> >> - part.php
>
> >> class Part extends AppModel {
>
> >> var $name = 'Part';
> >> var $useTable = 'parts';
>
> >> //The Associations below have been created with all
> >> possible keys,
> >> those that are not needed can be removed
>
> >> var $hasAndBelongsToMany = array(
> >> 'Model' => array('className' => 'Model',
> >> 'joinTable' =>
> >> 'models_parts',
> >> 'foreignKey' =>
> >> 'part_id',
>
> >> 'associationForeignKey' => 'model_id',
> >> 'unique' => true,
> >> 'conditions' => '',
> >> 'fields' => '',
> >> 'order' => '',
> >> 'limit' => '',
> >> 'offset' => '',
> >> 'finderQuery' => '',
> >> 'deleteQuery' => '',
> >> 'insertQuery' => ''
> >> )
> >> );
>
> >> }
>
> >> Now, the problem is, When I run find() from Parts Controller, I can
> >> get the data from table models_parts (So, I can tell which parts
> >> belongs to which models). However, I also want to be able to get
> >> data
> >> from table models_parts when I run find() from Models Controller.
>
> >> I have set both of these controllers to use recursive = 2 ($this-
> >> >Part-
> >> >recursive = 2, and $this->Model->recursive=2), yet it only works
> >> from Parts, not the model.
>
> >> Should I be creating a ModelPart model too?
>
> >> Please help me out, thank you :)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---