Your suggestions are great, but sadly, I've already tried combinations
of a blank foreign key as well as a foreign key of 'id'.
Example row from the states table:
+------+---------------+-----+
|id |name |code |
+------+---------------+-----+
|10 |new hampshire |nh |
+------+---------------+-----+
Example row from the registered_states table:
+----------+
| state_id |
+----------+
| 10 |
+----------+
-Erich-
Christopher E. Franklin, Sr. wrote:
> Well, it sounds good but, I always try to follow the K.I.S.S. method:
> Keep It Simple Stupid.
>
> I think the point now is not whether it is the "right way" or "wrong
> way" but, rather, why is it not working... if you or we can figure
> this out and post our findings, I think we would both be better coders
> for it.
>
> Give me an example row from each database and I will test out your
> code at work tomorrow.
>
> One thing I did notice about your hasMany array:
>
> from the example, this code:
>
> <?php
> class User extends AppModel
> {
> var $name = 'User';
> var $hasOne = array('Profile' =>
> array('className' => 'Profile',
> 'conditions' => '',
> 'order' => '',
> 'dependent' => true,
> 'foreignKey' => 'user_id'
> )
> );
> }
> ?>
>
> Outputs an array like:
>
> Array
> (
> [User] => Array
> (
> [id] => 25
> [first_name] => John
> [last_name] => Anderson
> [username] => psychic
> [password] => c4k3roxx
> )
>
> [Profile] => Array
> (
> [id] => 4
> [name] => Cool Blue
> [header_color] => aquamarine
> [user_id] = 25
> )
>
> Notice the foreign key. Let's say that user = registered_state and
> profile = state. Profile has a column with user_id. You are using
> state_id as your foreignKey yet, your table for state does not have a
> state_id... just an id. Try changing that foreign key for me to
> either blank or just id.
> )
>
> --Chris
>
> On Feb 11, 5:28 pm, "Erich C. Beyrent" <[EMAIL PROTECTED]>
> wrote:
>> I had added the belongsTo association to the states table, and that
>> didn't make a difference - I got the same results.
>>
>> Here's what I am trying to accomplish - visitors to the website must
>> select their state of residency to continue, and that state needs to be
>> validated against a list of registered states.
>>
>> In my current schema, I can accomplish the results I am looking for with
>> this query:
>>
>> select code from states right join registered_states on
>> registered_states.state_id = states.id
>>
>> This would give me a list like NH, MA, CT, RI, VT.
>>
>> I am looking for a way to do this through model associations and the
>> findAll method.
>>
>> I know that I could also accomplish this by adding an isRegistered field
>> to the states table with a value of 1 or 0, and then query on that.
>> Perhaps that is simply the easiest solution.
>>
>> Like I said, I know that I can use a custom query, but that seems
>> hackish when considering the capabilities of Cake.
>>
>> Any ideas?
>>
>> -Erich-
>>
>> Christopher E. Franklin, Sr. wrote:
>>
>>> If I am not mistaken, I believe the State model has to have an
>>> association with RegisteredState model as well to get that backwards
>>> compatibility. Take a look at the user and profile models in this
>>> example.
>>> http://manual.cakephp.org/chapter/models
>>> If you notice, User hasOne Profile while Profile belongsTo User.
>>> There is a definition in both models to get the expected return and
>>> association.
>>> I don't know exactly what you are trying to accomplish but, if you do
>>> a hasAndBelongsToMany, don't forget to make the Join table with the
>>> name registered_states_states and just have two columns, one with
>>> registered_state_id and the other with state_id.
>>> Tell me if this helps you any or, you can post back with what exactly
>>> you are trying to do and how you envision the relationship between
>>> state and registered_state. Are they sort of "tags" as in State:
>>> Open, Closed? or Something else?
>>> In all, the only problem I can see right now with your code is you do
>>> not have the backwards association in the State model. Try belongsTo.
>>> On Feb 11, 10:21 am, "Erich C. Beyrent" <[EMAIL PROTECTED]>
>>> wrote:
>>>> Here are my tables:
>>>> CREATE TABLE `registered_states` (
>>>> `state_id` int(11) NOT NULL default '0',
>>>> PRIMARY KEY (`state_id`)
>>>> ) ENGINE=MyISAM DEFAULT CHARSET=latin1
>>>> CREATE TABLE `states` (
>>>> `id` int(11) NOT NULL auto_increment,
>>>> `name` varchar(24) NOT NULL default '',
>>>> `code` char(2) NOT NULL default '',
>>>> PRIMARY KEY (`id`)
>>>> ) ENGINE=MyISAM DEFAULT CHARSET=latin1
>>>> Models:
>>>> // registered_state.php
>>>> class RegisteredState extends AppModel
>>>> {
>>>> var $name = 'RegisteredState';
>>>> var $hasMany = array('State' =>
>>>> array('className' => 'State',
>>>> 'conditions' => '',
>>>> 'order' => '',
>>>> 'limit' => '',
>>>> 'foreignKey' => 'state_id',
>>>> 'dependent' => false,
>>>> 'exclusive' => false,
>>>> 'finderQuery' => ''
>>>> )
>>>> );
>>>> }
>>>> // state.php
>>>> class State extends AppModel
>>>> {
>>>> var $name = 'State';
>>>> var $validates = array('code' => VALID_NOT_EMPTY);
>>>> }
>>>> -Erich-
>>>> Christopher E. Franklin, Sr. wrote:
>>>>> Post your model code with the names of the files and where they are
>>>>> located.
>>>>> On Feb 10, 6:12 pm, "Erich C. Beyrent" <[EMAIL PROTECTED]>
>>>>> wrote:
>>>>>> That did not work - now I get:
>>>>>> Array
>>>>>> (
>>>>>> [0] => Array
>>>>>> (
>>>>>> [RegisteredState] => Array
>>>>>> (
>>>>>> [state_id] => 10
>>>>>> )
>>>>>> [State] => Array
>>>>>> (
>>>>>> )
>>>>>> )
>>>>>> )
>>>>>> I know I can write my own custom query for this, but I am unsure as to
>>>>>> why this isn't working for me.
>>>>>> -Erich-
>>>>>> djiize wrote:
>>>>>>> in registered_states table, try state_id (not plural)
>>>>>>> On 9 fév, 19:10, "Erich C. Beyrent" <[EMAIL PROTECTED]> wrote:
>>>>>>>> I am trying to link two tables, states and registered_states.
>>>>>>>> The states table has id, name, and code as fields, and
>>>>>>>> registered_states
>>>>>>>> has states_id.
>>>>>>>> I defined as hasMany association in the registered_states model, and
>>>>>>>> now
>>>>>>>> want to produce a list of all the registered states and their
>>>>>>>> associated
>>>>>>>> state.
>>>>>>>> I'm getting the list of registered states, but no state data:
>>>>>>>> Array
>>>>>>>> (
>>>>>>>> [0] => Array
>>>>>>>> (
>>>>>>>> [RegisteredState] => Array
>>>>>>>> (
>>>>>>>> [states_id] => 10
>>>>>>>> )
>>>>>>>> [State] => Array
>>>>>>>> (
>>>>>>>> )
>>>>>>>> )
>>>>>>>> )
>>>>>>>> What I'm particularly interested in retrieving is the state code for
>>>>>>>> each registered state.
>>>>>>>> How can I accomplish this?
>>>>>>>> -Erich-
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---