Sasa
Sasa

Reputation: 563

Codeigniter doctrine

I try to use doctrine with Codeigniter.

I have two tables in db, authors and books.

Authors table has fields id, name and lastname. Books table has fields id, title and author_id.

I know how to write data into table, but I don't know how to get list of all authors and books which that author wrote. Can someone helps me with that?

Authors model

<?php
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @Entity @Table(name="authors")
 **/
class Author
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $name;
    /** @Column(type="string") **/
    protected $lastname;

    /**
     * @OneToMany(targetEntity="Book", mappedBy="author")
     */

    private $books;

    public function __construct()
    {
        $this->books = new ArrayCollection();
    }

    public function setAuthor($name,$lastname)
    {
        $this->name = $name;
        $this->lastname = $lastname;
    }

    public function authorName()
    {
        return $this->name.' '.$this->lastname;
    }

    public function updateAuthor($name,$lastname)
    {
        $this->name = $name;
        $this->lastname = $lastname;
    }

    public function setFirstName($name)
    {
        $this->name = $name;
    }

    public function setLastName($lastname)
    {
        $this->lastname = $lastname;
    }

    public function getBooks()
    {
        return $this->books;
    }

}

Book model

<?php
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @Entity @Table(name="books")
 **/
class Book
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $title;

    /**
     * @ManyToOne(targetEntity="Author", inversedBy="books")
     * @oinColumn(name="author_id", referencedColumnName="id")
     */

    private $author;

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function setAuthor($author)
    {
        $this->author = $author;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getAuthor()
    {
        return $this->author;
    }
}

Thanks in advance

Upvotes: 0

Views: 134

Answers (2)

Virendra Jadeja
Virendra Jadeja

Reputation: 873

You have an entity class Author and Book. So you can use this code. You can modify as per your need.

$authors = $em->getRepository("Author")->findAll();

Now you can get books of perticular author by calling getBooks() For example:

$authors = $em->getRepository("Author")->findAll();//$em is entity manager
    if (count($authors)):
        foreach ($authors as $author):
            $books= $author->getBooks(); //all books of current author
        endforeach;
    endif;

Advice: You should define namespace in your entities.

Upvotes: 0

Sunil Chhimpa
Sunil Chhimpa

Reputation: 404

I am giving you a model of select query using queryBuilder. Try to execute it.

    $query= $this->doctrine->em->createQueryBuilder();
    $query->select('bk.id book_id,au.id author_id');
    $query->add('from', 'Entities\Authors au');
    $query->Join('Entities\Book', 'bk', 'with', 'bk.author=au.id');
    $query_data =$query->getQuery();
    $data = $query_data->getResult();

You face any issue let me know

Advice- try to code yourself. If you face issue try to debug. if you can't, then only asks for help with whatever you tried.

Upvotes: 1

Related Questions