Implementing Model::relationships() #575

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
Contributor

jails commented Jul 13, 2012

Currently relation names are "Inflectorized" in datas, i.e. if you have the following relation : "Post HasMany Comment" the comment's data are available using :

$entity->comments

comments is the fieldname associated to the "Comment" relation but there's no way to reach the relation name without iterating over all Model's relations.

Model::relationship() allow here to get back to the relation name.

    echo Model::relationship('comments') // => 'Comment'

PS:
This will help to support the following syntax in the next futur :

$entity->comments = array(
    array('body' => 'comment1'),
    array('body' => 'comment1')
)
Owner

nateabele commented Jul 14, 2012

I'm not sure I understand why this is needed. Entity already contains all the mapping information it needs to support that syntax in $_relationships.

Contributor

jails commented Jul 15, 2012

Currently the syntax :

$document->comments = array(
    array('body' => 'comment1'),
    array('body' => 'comment1')
)

only works with Document if comments is correctly defined in the document's Schema class. comments can't be a relation like belongsTo/hasMany. And it doesn't work with Record, well i mean it will not be instantiated as a RecordSet.

$_relationships is only populated on finds, not on updates. It only contains loaded datas. So currently there's no way to get back to a relation name thanks to a field name. This PR result from #572.

It imo this 2 PR show another way to address relationships based on detection more than splits.

Contributor

jails commented Jul 15, 2012

Maybe a peace of code will be clearer..
The following is a draft of what I'm trying to reach for Record:
https://gist.github.com/3114191

Contributor

jails commented Jul 15, 2012

To summarize 572 & 573, these PR are a suggestion for changing the actual behavior where :

$_relationships = array(
    'fieldname' => //datas
    'fieldname' => //datas
    ...
);

$_updated = array(
    'fieldname' => //datas
    'fieldname' => //datas
    ...
);

(Note : $_relationships are only populated with datas if the 'with' option is used in finds).

To the following:

//Contain only the mapping between a fieldname and it's corresponding relation
$_relationships = array(
    'fieldname' => //its corresponding relation name
    'fieldname' => //its corresponding relation name
    ...
);

//All datas are in $_updated
$_updated = array(
    'fieldname' => //datas
    'fieldname' => //datas
    ...
);

(Note : here $_relationships is an exhausitve mapping between fieldnames & relations no matter this datas exists or not $_updated).

If we need to iterate over relation's data a simple array_keys() should does the job. Same behavior can be used for embeded

Owner

nateabele commented Jul 18, 2012

Any reason this can't be consolidated with Model::relations()?

Contributor

jails commented Jul 18, 2012

Imo it's just for performance since you can indeed did a foreach on all Model::relations() using a if ($fieldName == $relation->fieldName()) to know if a field name is a relation.
But currently I don't see any other alternative but may i miss the best one ;-)

@nateabele nateabele closed this Dec 11, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment