creatoro / jelly

A flexible ORM for Kohana 3.1+
http://jelly.jonathan-geiger.com
MIT License
72 stars 13 forks source link

ManyToMany on unsaved models #96

Closed ivank closed 6 years ago

ivank commented 12 years ago

When I do a $model->as_array() on an model that has not yet been created, I get an exception form here

    public function get($model, $value)
    {
        if ($model->changed($this->name))
        {
            return Jelly::query($this->foreign['model'])
                    ->where($this->foreign['field'], 'IN', $value);
        }

        // Set columns
        $join_col1 = $this->through['model'].'.'.$this->through['fields'][1];
        $join_col2 = $this->foreign['model'].'.'.$this->foreign['field'];
        $where_col = $this->through['model'].'.'.$this->through['fields'][0];

        // If the value hasn't changed, we need to pull from the database
        return Jelly::query($this->foreign['model'])
                    ->join($this->through['model'])
                    ->on($join_col1, '=', $join_col2)
                    ->where($where_col, '=', $model->id());
    }

The thing is - value is null so the >where($this->foreign['field'], 'IN', $value); fails There should be a empty($value) check here i think.

creatoro commented 12 years ago

@ivank: can you post example code? In what situation would you need this feature?

ivank commented 12 years ago

For basic $auth_user from jelly-auth

$user = Jelly::factory('user')->save();

Does not trigger validation error but rather throughs a general exception for the connection with user_tokens table

Database_Exception [ 1064 ]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1 [ SELECT `user_tokens`.`id` AS `id`, `user_tokens`.`user_agent` AS `user_agent`, `user_tokens`.`token` AS `token`, `user_tokens`.`type` AS `type`, `user_tokens`.`created` AS `created`, `user_tokens`.`expires` AS `expires`, `user_tokens`.`user_id` AS `user` FROM `user_tokens` AS `user_tokens` WHERE `user_tokens`.`id` IN NULL ]

we've dealt with it for now in Jelly_Core_Field_ManyToMany with

public function get($model, $value)
{
    if ($model->changed($this->name))
    {
        if($value)
            return Jelly::query($this->foreign['model'])
                    ->where($this->foreign['field'], 'IN', $value);
        else
            return $value;
    }

But I'm not sure it's the best course of action.