2

I have a tables named:

client (id, alias) post (id, subject) post_client (id, post_id, client_id)

Many clients can be joined to a post.

Using Zend DB Table abstract I have started to build a model, here are the classes:

ORM_Post

class ORM_Post extends Zend_Db_Table_Abstract {

    protected $_name = 'Post';
    protected $_dependentTables = array('ORM_Post_Client');

}

ORM_Client

class ORM_Client extends Zend_Db_Table_Abstract {


    protected $_name = 'Client';
    protected $_dependentTables = array(
        'ORM_Post_Client'
    );
}

ORM_Post_Client

class ORM_Post_Client extends Zend_Db_Table_Abstract {

    protected $_name = 'Post_Client';
    protected $_referenceMap    = array(
        'post' => array(
            'columns'           => 'post_id',
            'refTableClass'     => 'ORM_Post',
            'refColumns'        => 'id'
        ),
        'client' => array(
            'columns'           => 'client_id',
            'refTableClass'     => 'ORM_Post_Client',
            'refColumns'        => 'id'
        )

    );
}

What I was hoping todo is call an instance of the Post and then load the clients associated aswell as loading an instance of the client and load all posts associated.

So I did this:

    $post = new ORM_Post();
    $results = $post->fetchAll();

    foreach ($results as $key => $result){
        $row = $results->current();
        $client = $row->findDependentRowset('ORM_Post_Client','client');
    }

and I get Reference rule "client" does not reference table ORM_Post

I have battled with this for hours and cannot see where I'm going wrong. Am I to declare the Post_Client joins inside the client and post model also?

EDIT

Here is what I was after:

    $post = new ORM_Post();
$results = $post->fetchAll();
$return = array();

    foreach ($results as $result){
        $row = $post->find($result->id)->current();
        $return[$result->id] = $row->toArray();
        $return[$result->id]['clients'] = $row->findManyToManyRowset('ORM_Client', 'ORM_Post_Client')->toArray();
}

return $return;

Thanks for the advice guys, you put me on the right track

2 Answers 2

3

in your ORM_Post_Client it should be

'client' => array(
            'columns'           => 'client_id',
            'refTableClass'     => 'ORM_Client',  //instead of ORM_Post_Client 
            'refColumns'        => 'id'
        )

refTableClass => The class name of the parent table. Use the class name, not the physical name of the SQL table (documentation)

also i think your loop should be :

foreach ($results as $result){
        $row = $results->current();
        $clients = $row->findDependentRowset('ORM_Post_Client','post'); 
  }

because you are looking for clients of a post which means that post is your rule

($row->findDependentRowset($table, [$rule]); )

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for spotting this. However it doesn't fix the issue. I get: Reference rule "client" does not reference table ORM_Post
0

This as presented won't work, honestly it makes no sense.

$post = new ORM_Post();
    $results = $post->fetchAll();

    foreach ($results as $key => $result){
        //$row is assigned to the whole fetchall result!
        $row = $results->current();
        //in this context $client cannot call a dependent rowset.
        $client = $row->findDependentRowset('ORM_Post_Client','client');
    }

MMc is correct in that you reference table definition was incorrect however your code has some issues as well. Maybe try something like:

 $post = new ORM_Post();

    $results = $post->fetchAll();

    //unless your are going to use the 'key' for something you don't need it
    foreach ($results as $result){

        //you need each row object in order to call findDependentRowset in a one to many relationship.
        $row = $post->find($result->id)->current();
        //unless you have multiple rules set up for each table class pair you don't need to specify the rule.
        $client = $row->findDependentRowset('ORM_Post_Client');
    }

3 Comments

I get a bunch of: Notice: Object of class Zend_Db_Table_Row could not be converted to int in /Users/-/Sites/GIT/api/library/Zend/Db/Adapter/Abstract.php on line 853
@azz0r I made an edit I forgot to specify which property of each result we were looking for. $result->id should work or if the property is not defined $result['id']. where id is the id field of your table. Sorry for the oops.
This really explained fairly well at mattmccormick.ca/2010/04/24/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.