var $uses = array('Store', State');

If you add a 2nd model you have to include both of them in $uses. If
the variable is present, Cake will not bother figuring it out for you.
Thus, Store is not found.

Incidentally, I changed States to State. The model name should
generally be singular.

On Sun, May 3, 2009 at 12:01 PM, PCH <philip.hew...@gmail.com> wrote:
>
> I'm having problems with this solution.
> I've got my child model - state.php
> class State extends AppModel {
>        var $name = 'State';
>        var $hasMany = array('Store' => array('className' => 'Store',
> 'foreignKey' => 'store_id'));
> }
>
> I've got my parent model - store.php
> class Store extends AppModel {
>
>        var $name = 'Store';
>        var $validate = array('store' => array('rule' => VALID_NOT_EMPTY,
> 'message' => 'Store name cannot be empty'));
>        var $belongsTo = array('State' => array('className' => 'State',
> 'foreignKey' => 'state_id'));
>
> }
>
> And I've got my parent controller - stores_controller.php
> class StoresController extends AppController {
>        var $name = 'Stores';
>        var $uses = array('States');
>
>        function index() {
>                $this->set('stores', $this->Store->find('all'));
>                $this->pageTitle = 'Stores';
>        }
>
>        function add() {
>                $this->pageTitle = 'Add a new Store';
>
>                if (!empty($this->data)) {
>                        $this->Store->create();
>                        if ($this->Store->save($this->data)) {
>                                $this->Session->setFlash(__('The Store has 
> been saved'));
>                                $this->redirect(array('action' => 'index'));
>                        } else {
>                                $this->Session->setFlash(__('Store not saved. 
> Try again.', true));
>                        }
>                }
>                $states = $this->State->find('list');
>                $this->set(compact('states', $states));
>        }
>
>        function edit($id = null) {
>        }
>
>        function delete($id = null) {
>        }
> }
>
> I get the following error when I invoke the index function
> Notice (8): Undefined property: StoresController::$Store [APP
> \controllers\stores_controller.php, line 7]
>
> Fatal error: Call to a member function find() on a non-object in S:
> \wwwserver\mypricebook\app\controllers\stores_controller.php on line
>
> The line 7 code is:
> $this->set('stores', $this->Store->find('all'));
>
> Any ideas what I'm missing or doing wrong?
>
> Thanks
> Phil - super CakePHP noob
>
>
> On May 2, 11:06 pm, brian <bally.z...@gmail.com> wrote:
>> Try this:
>> $allies = $this->Ally->find( 'all' );
>>
>> On Sat, May 2, 2009 at 10:31 PM, craig.kaminsky
>>
>> <craig.kamin...@gmail.com> wrote:
>>
>> > Hi,
>>
>> > I'm a CakePHP noob who is converting a site from Fusebox/ColdFusion to
>> > CakePHP (1.2.x). Without boring anyone with the domain details, I want
>> > to output a list of 'allies' and their associated organizations
>> > (belongs to an organization in the model). The problem I am running
>> > into is that I cannot get CakePHP to recognize the relationship
>> > between Allies and Organizations from a "third-party" controller (the
>> > "HomeController").
>>
>> > Here's my setup:
>>
>> > MODEL:
>> > FILENAME: ally.php
>> > class Ally extends AppModel {
>>
>> >        var $name = 'Ally';
>>
>> >        //The Associations below have been created with all possible keys,
>> > those that are not needed can be removed
>> >        var $belongsTo = array(
>> >                'Organization' => array(
>> >                        'className' => 'Organization',
>> >                        'dependent' => true,
>> >                        'foreignKey' => 'organization_id'
>> >                )
>> >        );
>> > }
>>
>> > FILENAME: organization.php
>> > class Organization extends AppModel {
>>
>> >        var $name = 'Organization';
>> >        var $displayName = 'name';
>>
>> >        //The Associations below have been created with all possible keys,
>> > those that are not needed can be removed
>> >        var $belongsTo = array(
>> >                'OrganizationType' => array(
>> >                        'className' => 'OrganizationType',
>> >                        'foreignKey' => 'organization_type_id'
>> >                )
>> >        );
>>
>> >        var $hasMany =  array(
>> >                'Ally' => array(
>> >                        'className' => 'Ally',
>> >                        'foreignKey' => 'organization_id'
>> >                )
>> >        );
>>
>> > }
>>
>> > CONTROLLER:
>> > FILENAME: home_controller.php
>> > class HomeController extends AppController
>> > {
>> >        var $name = 'Home';
>> >        var $uses = array( 'Allies');
>>
>> >        function index()
>> >        {
>> >                $allies = $this->Allies->find( 'all' );
>> >                $this->set( 'allies', $allies );
>> >        }
>>
>> > }
>>
>> > DB:
>> > The tables (allies and organizations) are plural and both have a
>> > primary key named 'id". The allies table has a column called
>> > 'organization_id' to act as a foreign key (MySQL 5 DB).
>>
>> > RESULTING SQL IN DEBUG OUTPUT:
>> > SELECT `Allies`.`id`, `Allies`.`organization_id`, `Allies`.`sfa_href`,
>> > `Allies`.`logo`, `Allies`.`overview`, `Allies`.`active` FROM `allies`
>> > AS `Allies` WHERE 1 = 1
>>
>> > Why wouldn't this setup/configuration result in the allies select
>> > trying to join the organizations table from the home controller?
>>
>> > It does work properly when I set it up from the Allies controller in
>> > that I see both Ally data and Organization data. I'm just a little
>> > stumped as to how to make this work from the HomeController.
>>
>> > Any help or guidance would be much appreciated.
>>
>> > Thanks!
>> > Craig
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to