HYDRATION like in Doctrine ORM in Symfony Framework

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

HYDRATION like in Doctrine ORM in Symfony Framework

aoohralex
I have in database two tables:
books:
- id PK
- title

authors:
- id PK
- author
- id_book FK

1 book can have many authors. I want to learn ZF so database doesn't matter (I know it should be that also 1 author can have many books and it shouldn't be column 'author' but column 'name' and 'surname').

I have in model:
public function getBooksAuthors()
{
$select = $this->select()
->setIntegrityCheck(false)
->from(array('b' => 'books'))
->join(array('a' => 'authors'), 'b.id = a.id_book');
}

I have in controller:
public function indexAction()
{
                $books = new Model_DbTable_Books();
                $this->view->list = $books->getBooksAuthors();
}
       
I have in view:
<?php foreach($this->list as $row): ?>
<?php echo $row['title']; ?> <?php echo $row['author']; ?>
<br/>     
<?php endforeach; ?>


And I see:
Book 1 Ben Johnson
Book 1 Michael Great
Book 2 Alexander Mortensen

And I would like to see:
Book 1 Ben Johnson Michael Great
Book 2 Alexander Mortensen

In Symfony Framework and Doctrine ORM it was something called HYDRATION. I could do this using something like this:
foreach ($list['Books'] as $book)
{
  echo $book['title'];
  foreach ($book['Authors'] as $author)
  {
    echo $row['author'];
  }
  echo '<br/>';
}


How can I do this in ZF ?
Reply | Threaded
Open this post in threaded view
|

Re: HYDRATION like in Doctrine ORM in Symfony Framework

Vladas Diržys
ZF does't support such a feature yet. But you can achieve the same result by building the right SQL query your self.

I think your getBooksAuthors should look like this:

public function getBooksAuthors()
{
    $select = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('b' => 'books'), array('id'))
            ->join(array('a' => 'authors'), 'b.id = a.id_book', array('authors'=>new Zend_Db_Expr('
GROUP_CONCAT(author)')))
            ->group('b.id');
}

--
Regards,
Vladas Diržys



On Thu, Sep 17, 2009 at 15:07, aoohralex <[hidden email]> wrote:

I have in database two tables:
books:
- id PK
- title

authors:
- id PK
- author
- id_book FK

1 book can have many authors. I want to learn ZF so database doesn't matter
(I know it should be that also 1 author can have many books and it shouldn't
be column 'author' but column 'name' and 'surname').

I have:
public function getBooksAuthors()
{
$select = $this->select()
->setIntegrityCheck(false)
->from(array('b' => 'books'))
->join(array('a' => 'authors'), 'b.id = a.id_book');
}


public function indexAction()
{
               $books = new Model_DbTable_Books();
               $this->view->list = $books->getBooksauthors();
}


<?php foreach($this->list as $row): ?>
<?php echo $row['title']; ?> <?php echo $row['author']; ?>
<br/>
<?php endforeach; ?>


And I see:
Book 1 Ben Johnson
Book 1 Michael Great
Book 2 Alexander Mortensen

And I would like to see:
Book 1 Ben Johnson Michael Great
Book 2 Alexander Mortensen

In Symfony Framework and Doctrine ORM it was something called HYDRATION. I
could do this using something like this:
foreach ($list['Books'] as $book)
{
 echo $book['title'];
 foreach ($book['Authors'] as $author)
 {
   echo $row['author'];
 }
 echo '<br/>';
}


How can I do this in ZF ?
--
View this message in context: http://www.nabble.com/HYDRATION-like-in-Doctrine-ORM-in-Symfony-Framework-tp25490065p25490065.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Reply | Threaded
Open this post in threaded view
|

Re: HYDRATION like in Doctrine ORM in Symfony Framework

aoohralex
hm....... it doesn't work for me:
Specified column "author" is not in the row
Reply | Threaded
Open this post in threaded view
|

Re: HYDRATION like in Doctrine ORM in Symfony Framework

Vladas Diržys
Try with Zend_Db::fetchAll(). Your method then, should look like this :

public function getBooksAuthors()
{
$select = $this->select()
->setIntegrityCheck(false)
->from(array('b' => 'books'), array('id'))
->join(array('a' => 'authors'), 'b.id = a.id_book', array('authors'=>new Zend_Db_Expr('GROUP_CONCAT(author)')))
->group('b.id');
return $this->getAdapter()->fetchAll($select);
}


--
Regards,
Vladas Diržys


On Thu, Sep 17, 2009 at 16:06, aoohralex <[hidden email]> wrote:

hm....... it doesn't work for me:
Specified column "author" is not in the row
--
View this message in context: http://www.nabble.com/HYDRATION-like-in-Doctrine-ORM-in-Symfony-Framework-tp25490065p25490884.html
Sent from the Zend Framework mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: HYDRATION like in Doctrine ORM in Symfony Framework

aoohralex
Zend_Db_Expr('GROUP_CONCAT(author)')  - this is strange for me, I don't understand that so I don't want to use it - I MUST know and understand all code (I know that in ZF it isn't possible).

getAdapter() - I also don't know that method.

And I don't know how I can use it to show rows in that way as I want.


This is next strange thing in ZF....

I think I will stay with that what I got but thx for trying help.