jonathangeiger / kohana-jelly

See the link below for the most up-to-date code
https://github.com/creatoro/jelly
MIT License
146 stars 34 forks source link

Get ManyToMany not saved relationships #147

Closed Neurergus closed 14 years ago

Neurergus commented 14 years ago

Hi,

I have a model with a ManyToMany field, activities. In the database, model 'A' has the activities 1, 2, 3.

$has_activity1 = $A->has('activities', 1); // TRUE
$has_activity7 = $A->has('activities', 7); // FALSE

Okay. Now suppose I do this:

$A->set('activities', array(5, 6, 7));

$new_activities = $A->get('activities')->select()->as_array('id'); // Okay: 5, 6, 7

$has_activity5 = $A->has('activities', 5); // FALSE
$has_activity1 = $A->has('activities', 1) // TRUE

I don't know if that's the desired behavior, but in my case I need to know if the model has a relationship prior to saving it, something like:

if ($model->changed('field'))
{
    // Look in the new setted values
}
else
{
    // Look in the database
}

What do you think?

banks commented 14 years ago

I'm trying to get clear what the problem is here. Am I right in thinking that you are assigning a new value to a many relationship and that having done so, the has() method returns based on the original value and not the changed value?

Does it work as expected after save?

If that is what is going on then I agree it is not what I would expect and probably needs to be 'fixed' if it isn't already in the unstable branch.

Neurergus commented 14 years ago

Yes bank, you're right.

After saving, it works like expected. But in this particular case saving (at that moment) is ruled out.

Now I'm using this extended class for my needs:

class Jelly_Field_ManyToMany extends Jelly_Core_Field_ManyToMany
{
    protected function _in($model, $as_array = FALSE)
    {
        if ( ! $model->changed($this->name))
        {
            return parent::_in($model, $as_array);
        }

        return $this->_ids($model->{$this->name});
    }
}

By the way, it's the unstable branch.

jonathangeiger commented 14 years ago

Closed by 54f5fc2cddeda953dd5a2797308ebcea51aef50e. Thanks Cataphractus!