1

I have two entities (Item and Products).

One item has only one product. One product could have 0 or N items.

When I Create a new Item, all is right except product_id is null, but in dev environment product_id is right.

class Item{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;    

/**
 * @ORM\ManyToOne(targetEntity="Products", inversedBy="item", cascade={"persist"})
 * @ORM\JoinColumn(name="product_id", referencedColumnName="product_id", onDelete="cascade")
 * @var My\WebBundle\Entity\Products
 */  
protected $products;  

 /**
 * Get Id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
* Set products
* 
* @param My\WebBundle\Entity\Products $products
*/
public function setProducts(\My\WebBundle\Entity\Products $products)
{
    $this->products = $products;

    return $this;
}

/**
* get products
* 
* @return My\WebBundle\Entity\Products
*/
public function getProducts()
{
    return $this->products;
}

}

Class Product

class Products
{
/**
 * @var integer
 *
 * @ORM\Column(name="product_id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $productId;

/**
* @ORM\OneToMany(targetEntity="Item", mappedBy="products", cascade={"persist", "remove"})
*/
protected $item;

/**
 * Get productId
 *
 * @return integer 
 */
public function getProductId()
{
    return $this->productId;
}

public function addItem(\My\WebBundle\Entity\Item $item)
{
    $this->item[] = $item;
    $item->setProducts($this);
    return $this;
}

/**
 * Get item
 *
 * @return Doctrine\Common\Collections\Collection
 */
public function getItem()
{
    return $this->item;
}
}

My problem is this, when I create this Item I use:

$p = $em->getRepository('MyWebBundle:Products')->findOneByCode($code);                
$item->setProducts($p);  

In my database, in Item table, product_id is null, but if I do this in dev environment product_id is right.

Anybody have any idea? Thanks in advance.

---edit--- I have a simple controller like this:

$em = $this->getDoctrine()->getManager();
$item = new Item();
$item->setName($name);
$p = $em->getRepository('MyWebBundle:Products')->findOneByCode($product);                
$item->setProducts($p); 
$em->persist($item);
$em->flush()
user3396420
  • 810
  • 2
  • 13
  • 34
  • 1) Have you cleared the cache? 2) Are you using different database in `dev` than in `prod` mode? – Jovan Perovic May 27 '14 at 12:12
  • Thanks, but yes, I cleaned the cache, and the database it's the same, (I have other fields and all are generated right) – user3396420 May 27 '14 at 12:15
  • Ah OK. It seems that you have complied with every rule concerning `owning` and `inverse` sides of a relationship. Can you show us what else are you trying to do with those entities (some more code)? – Jovan Perovic May 27 '14 at 12:28
  • Yes, see my simple controller, only I put name and product, and name is right but product doesn't appear. – user3396420 May 27 '14 at 12:40
  • I honestly do not see what might be wrong here. :-/ Assuming that repository method returns managed instance of product everything should be find. Could `onDelete` constraint be messing up the relation? Either way, I'm upvoting this in hope it will get some attention... – Jovan Perovic May 27 '14 at 12:53
  • Thanks a lot jperovic, I did something similar and all works fine, but in this case I don't know what is the problem. Thanks! – user3396420 May 27 '14 at 12:57
  • While it might be intuitive I would suggest adding tables' structure to your question, just in case... ;) – Jovan Perovic May 27 '14 at 13:02
  • Thanks, although in this case tables have been create using doctrin:schema:update – user3396420 May 27 '14 at 13:06
  • This won't change much but if 1 Item can only have 1 product, then use `$product` without the extra 's' and in Product, use `$items` :) – Rybus May 27 '14 at 13:52

0 Answers0