Check if UNIQUE row already exists using findBy in

2019-09-03 15:14发布

问题:

So I've this relations defined in my entities:

class Producto
{
    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Norma", inversedBy="normasProducto", cascade={"persist"})
     * @ORM\JoinTable(name="nomencladores.norma_producto", schema="nomencladores",
     *      joinColumns={@ORM\JoinColumn(name="producto_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="norma_id", referencedColumnName="id")}
     * )
     */
    protected $productoNormas;

} 


class Norma
{
    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Producto", mappedBy="productoNormas", cascade={"persist"})
     */
    protected $normasProducto;
}

And I'm trying to check if a given pair producto_id-norma_id already exists for not try to insert it once again and I'm doing as follow:

$exists = $em->getRepository('AppBundle:Producto')->findOneByProductoNormas($productoId);

if ($exists) {
    $status = 400;
} else {
    try {
        $producto->addProductoNormas($normaEntity);
        $em->flush();
        $response['success'] = true;
    } catch (Exception $ex) {
        $status = 400;
        $response['error'] = $ex->getMessage();
    }
}

But I'm getting this error:

Notice: Undefined index: joinColumns in /var/www/html/project.dev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1665

Doing a research on Google and here on SO I found that possible it's a bug as point here or perhaps is not and we (me and the others who report the issue) are doing something wrong. I can't find where the issue or problem is so any advice or help will be fine for me and others. The only idea I have in mind is create a view at RDBMS and then create a entity for read it and check if record already exists, I have not other than this one, any ideas? Help? Working example code?

回答1:

Actually you can use 'database_connection' service to check if such row exists:

$this->get('database_connection')
     ->fetchColumn('select count(id) as cnt 
                    from <norma_producto_table> 
                    where producto_id = ? and 
                          norma_id = ?', array($producto_id, $norma_id));

That's really easier than trying to handle this with many to many relations methods. I would do that if I had to do what you need (and actually I'm doing so).



回答2:

I don't know the definitive answer but i had the same problem and it had something to do with my entities annotations can't tell you exactly what though..

Here is a working example with photos and albums

class Photo
    {
         /**
         * @ORM\ManyToMany(targetEntity="Acme\MyBundle\Entity\Album", inversedBy="photos")
         * @ORM\JoinColumn(nullable=true)
        */
        private $albums;

class Album
    {
        /**
         * @ORM\ManyToMany(targetEntity="Acme\MyBundle\Entity\Photo", mappedBy="albums")
         * @ORM\JoinColumn(nullable=true)
        */
        private $photos;

In order to check the existence you could do, let's say you're searching for an idPic photo

$photo = $repository ... ->findOneBy($idPic)
if($photo->getAlbums()->contains($album))