Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorwmcdona89
     
    I have two models, video and label, that each have a HasAndBelongsToMany relationship with the other.

    class Video extends ActiveRecord
    var $has_and_belongs_to_many = 'labels';

    class Label extends ActiveRecord
    var $has_and_belongs_to_many = 'videos';

    Sample code:
    $this->video =& $this->Video->find($cur_video);
    $label =& $this->video->label->find($label_id);

    Error:
    labels.id = 1 AND SELECT labels.* FROM labels LEFT OUTER JOIN labels_videos ON labels_videos.label_id = labels.id LEFT OUTER JOIN videos ON labels_videos.video_id = videos.id WHERE videos.id = 3
    1064: You have an error in your SQL syntax;

    In the AkHasAndBelongsToMany->find() method, the 'finder_sql' is put in the $options['conditions'] element and passed to the AkActiveRecord->find() method. The problematic query is then formed in the AkActiveRecord->_findByIds() method.

    In AkActiveRecord->_findByIds()
    $conditions = !empty($options['conditions']) ? ' AND '.$options['conditions'] : ''; //prefixed with 'AND'
    and a few lines later:
    $options['conditions'] = $table_name.'.'.$this->getPrimaryKey().' = '.$ids[0].$conditions;

    I added some code to put the 'AND' statement at the end of the query if $options['conditions'] starts with 'SELECT' at this point.

    Am I doing something wrong or is this definitely a bug?

    Also, the example in the heading of AkHasAndBelongsToMany.php states that the find method is accessible from Developer->projects
    Shouldn't this be Developer->project (no 's')
    <tt>Developer->projects->find($id)</tt>
    • CommentAuthorKaste
     

    Thats a bug, certainly!

    AkHasAndBelongsToMany->find() treats 'finder_sql' as condition though it is actually a complete sql-select-statement.

    Shouldn't this be something like:

    $options['joins'] = $this->associationJoin();
    $current_id_clause = $this->Owner->getTableName().'.'.$this->Owner->getPrimaryKey()." = ".$this->Owner->quotedId();
    $options['conditions'] = empty($options['conditions']) ? $current_id_clause : $options['conditions'].' AND '.$current_id_clause;
    

    instead of 766..

    Beside that, the passage smells: it duplicates code from the AkActiveRecord and AkHasMany and overall is hard to read.