Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthormatiasjg
     
    Hi, first of all sory of my english, I speak spanish.
    I am having some problems making relationships of the same model.
    So, I have an "Application" model that "has and belongs to many" "Applications".

    tables:


    CREATE TABLE `applications` (
    `id` int(11) NOT NULL auto_increment,
    `code` varchar(100) NOT NULL,
    `name` varchar(100) default NULL,
    PRIMARY KEY (`id`)
    )

    CREATE TABLE `applications_applications` (
    `id` int(11) NOT NULL,
    `application_id` int(11) NOT NULL,
    `chain_id` int(11) NOT NULL,
    PRIMARY KEY (`id`)
    )


    here my code:

    var $has_and_belongs_to_many = array(
    'applications' => array(
    'unique'=>true,
    'foreign_key' => 'chain_id', /* id for relation */
    'association_foreign_key' => 'application_id', /* I don't understand the order, application_id is the father and chain_id is the child, but it doesn't matter because the relationship isn't running, the error is before I supouse */
    )
    );



    unit test:


    $Application = new Application();
    $Application1 = $Application->find(44);
    $this->assertFalse($Application1->isNewRecord());

    $Application2 = $Application->find(47);
    $this->assertFalse($Application2->isNewRecord());

    $Application3 = $Application->find(37);
    $this->assertFalse($Application3->isNewRecord());

    $ApplicationChain =& new ApplicationChain(); /*this one extends from class: Application and adds the relation explained before */
    $ApplicationChain->name='testeo modulo test unit';
    $ApplicationChain->code='testeo.modulo.test.unit';

    $ApplicationChain->application->load();
    $this->assertEqual($ApplicationChain->application->count(), 0);

    $ApplicationChain->application->add($Application1);
    $this->assertEqual($ApplicationChain->application->count(), 1);

    $ApplicationChain->application->add($Application2);
    $this->assertEqual($ApplicationChain->application->count(), 2);

    $ApplicationChain->application->add($Application3);
    $this->assertEqual($ApplicationChain->application->count(), 3);

    $this->assertTrue($ApplicationChain->save()); /* up to here all is ok, the save function makes the new record but not relations */

    I have to make a fix over AkHasAndBelongsToMany

    line 589:
    "WHERE owner.".$this->Owner->getPrimaryKey()." ".
    instead of
    "WHERE ".$this->Owner->getTableName().'.'.$this->Owner->getPrimaryKey()." ".

    line 608 & 609:
    "LEFT OUTER JOIN ".$this->Owner->getTableName()." as owner ON ".
    "{$options['join_table']}.{$options['foreign_key']} = owner.".$this->Owner->getPrimaryKey()." ";
    instead of
    "LEFT OUTER JOIN ".$this->Owner->getTableName()." ON ".
    "{$options['join_table']}.{$options['foreign_key']} = ".$this->Owner->getTableName().".".$this->Owner->getPrimaryKey()." ";

    /*****************/
    Note: I have to add "as owner" because it duplicate table owner: 'application' with the left outer join: 'application' and crash


    the error withot "as owner" :
    Unexpected PHP error [Tried 'SELECT applications.* FROM applications LEFT OUTER JOIN applications_applications ON applications_applications.application_id = applications.id LEFT OUTER JOIN applications ON applications_applications.chain_id = applications.id WHERE applications.id = 65 AND ( applications.type = 'Application' '. Got: [1066] Not unique table/alias: 'applications'] severity [E_USER_NOTICE] in [*******akelos/lib/AkActiveRecord/AkDbAdapter.php line 226

    /*****************/


    well, any ideas????? ..........



    thanks!!
    •  
      CommentAuthorbermi
     

    Hi Matias,

    You should save $ApplicationChain before adding associates.

    • CommentAuthormatiasjg
     
    well, I found a solution, I made a new model intermidiate and replace the habtm with belongs_to and has many, and I suggest to implement the rails solution: "throught", I couldn't find it in akelos yet

    cya