Symfony & Doctrine: Undefined index when trying to access one-to-many - php

Im having a bit of problems.
I have 2 classes:
Carorder:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Orderdetail;
/**
* #ORM\Entity
* #ORM\Table(name="carorder")
*/
class Carorder
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="Orderdetail", mappedBy="Carorder", cascade={"persist","remove"})
**/
protected $orderdetails;
//Then all the auto genereted setters and getters beneath here
Orderdetail:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** #ORM\Entity
* #ORM\Table(name="orderdetail")
*/
class Orderdetail
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Carorder", inversedBy="orderdetails")
**/
protected $carorder;
/**
* #ORM\Column(type="integer")
*/
protected $amount;
//Then all the auto generated setters and getters beneath here
I can't access the orderdetail through the Carorder. For example this example, just thorws the
Undefined index: Carorder
Example:
$repository = $this->getDoctrine()->getRepository('AppBundle:Carorder');
$orders = $repository->findAll();
$orderdetail = $orders[0]->getOrderdetails()->first();
I have no idea what is causing this, so i hoped you guys could help me out.

You mapped the property Carorder but your property name is carorder, it's case sensitive.
A correct mapping could be:
/**
* #ORM\OneToMany(targetEntity="Orderdetail", mappedBy="carorder", cascade={"persist","remove"})
**/
protected $orderdetails;

Related

Create columns for inherited fields in child-class usine Doctrine 2 ORM

I use Symfony 3. I've got a parent class Licence which has some fields:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Licence
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(
* type="string",
* unique=true,
* length=30,
* nullable=false
* )
*/
protected $licenceKey;
}
And I have a child class which extends this one. Surely, it has the same field but I need them to be stored in database in a specific place:
So I wrote the following:
namespace AppBundle\Entity\LicenceCase\FirstLicenceCase;
use AppBundle\Entity\Licence as BaseLicence;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="lic_first")
* #ORM\InheritanceType("SINGLE_TABLE")
*/
class FirstLicenceCase extends BaseLicence
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
public function getLicenceKey()
{
return $this->licenceKey;
}
public function setLicenceKey($licenceKey)
{
$this->licenceKey = $licenceKey;
}
}
I need to get the unique columns for any specific LicenceCase. I am going to create new class for every LicenceCase with different fields and methods. But with this code I don't get a proper columns in my database. How can I solve this problem?
The solution will be to specify Inheritance tree where I need to specify all children. e.g in parent class to write the following annotations.
/**
* #Entity
* #InheritanceType("SINGLE_TABLE")
* #DiscriminatorColumn(name="discr", type="integer")
* #DiscriminatorMap({"1" = "LicenceCase1", "2" = "LicenceCase2", "3" = "LicenceCase3"})
*/
abstract class Licence
{
// ...
}
This works for me.

Doctrine: the target-entity cannot be found in (two bundles)

I'm trying to do a ManyToOne relation in Symfony2 with Doctrine. My entities are:
namespace My\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="company")
*/
class Company
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
public function __construct() {
$this->users = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
And the other one:
namespace My\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
}
/**
* #var
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
/**
* #ORM\ManyToOne(targetEntity="My\ApiBundle\Entity\Company")
* #ORM\JoinColumn(name="idLastCompany", referencedColumnName="id")
*/
protected $idLastCompany;
Obviusly with other attributes and their sets and gets methods, but my only relation is between Company with idLastCompany. When I clear cache for example, I get this error:
MappingException: The target-entity My\ApiBundle\Entity\Copmany cannot
be found in 'My\UserBundle\Entity\User#idLastCompany'.
Any Idea?
Thanks
The error message tells you everything you need :)
MappingException: The target-entity My\ApiBundle\Entity\ Copmany cannot be found in 'My\UserBundle\Entity\User#idLastCompany'.
You spelled CoPMany instead of Company either in the entity file name, in the entity class name or in the relation field $idLastCompany docblock.
Even though the code you posted here is correct, your actual code contains a typo.
I would search the entire project for "Copmany" and fix the typo. Then it will work.

Property not defined while extends from BaseUser at FOSUserBundle

I have this User entity extends from BaseUser:
namespace PDOneBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use PDOneBundle\Classes\Form\Validator\Constraints as GdcaAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
* #ORM\Table(name="fos_user")
* #ORM\Entity(repositoryClass="PDOneBundle\Entity\Repository\UserRepository")
* #Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class User extends BaseUser
{
/*
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var int
*
* #Assert\NotBlank()
*
* #ORM\ManyToOne(targetEntity="Company", inversedBy="users", cascade={"persist"})
* #ORM\JoinColumn(name="company_id", referencedColumnName="ID")
*/
protected $company;
/**
* #ORM\ManyToMany(targetEntity="Company", mappedBy="admins", cascade={"persist"})
*/
protected $companies;
/**
* #ORM\ManyToMany(targetEntity="Project", mappedBy="admins", cascade={"persist"})
*/
protected $projects;
// methods goes here
}
I am using also EasyAdminBundle and any time I try to add a new user from EasyAdmin I got the following error:
Neither the property "expiresAt" nor one of the methods
"getExpiresAt()", "expiresAt()", "isExpiresAt()", "hasExpiresAt()",
"__get()" exist and have public access in class
"PDOneBundle\Entity\User".
Why? It is not supposed that those methods are extended from BaseUser? Why the error?
It doesn't look like there is a getter for property expiresAt : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Model/User.php
I guess you could easily add your own getter in your extended User class if EasyAdminBundle requires it.
class User extends BaseUser
{
// ...
public function getExpiresAt() {
return $this->expiresAt;
}
// ...
}
Looks like you could also define which properties EasyAdminBundle controls : https://github.com/javiereguiluz/EasyAdminBundle/blob/v1.2.1/Resources/doc/11-configuration-reference.md#advanced-configuration-with-custom-field-configuration

PUGXMulti-user-Bundle: Entity class does not exist

I'm trying to implement PUGXMultiUserBundle in a Sylius install.
After following bundle's instructions I'm able to fill the form for register userOne but all I get is:
'Entity class 'UserOne' used in the discriminator map of class 'UserBundle\Entity\User' does not exist.
As explained in bundle's documentation, my User.php:
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* #ORM\Entity
* #ORM\Table(name="sylius_user")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"userOne" = "UserOne", "userTwo" = "UserTwo"})
*/
abstract class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
And my userOne.php class:
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #ORM\Table(name="userOne")
* #UniqueEntity(fields = "username", targetClass ="UserBundle\Entity\User", message="fos_user.username.already_used")
* #UniqueEntity(fields = "email", targetClass ="UserBundle\Entity\User", message="fos_user.email.already_used")
*/
class UserOne extends User {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
I would appreciate any feedback. I can't generate the entities and/or complete the registration due to the same error.
Any help would be really appreciated.

Symfony2 - Doctrine - Entity relationship between 2 bundles

I can't figure out how i can have an entity relationship between 2 bundles.
The closest i've been to succeed so far is this:
User Entity:
<?php
namespace Acme\AuthBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\generatedValue(strategy="AUTO")
*/
protected $id;
//...
/**
* #ORM\OneToMany(targetEntity="Acme\NewsBundle\Entity\Article", mappedBy="author")
*/
protected $articles;
//...
Article Entity:
<?php
namespace Acme\NewsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="Acme\NewsBundle\Entity\ArticleRepository")
* #ORM\Table(name="articles")
*/
class Article
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//...
/**
* #ORM\ManyToOne(targetEntity="Acme\AuthBundle\Entity\User", inversedBy="articles")
*/
protected $author;
//...
When generating entities and updating the database, i get no errors and the relation is correctly set in the DB.
But when I try to fetch the articles i get this:
Class Acme\AuthBundle\Entity\Article does not exist
500 Internal Server Error - ReflectionException
Note that the User entity is in AuthBundle and Article entity in NewsBundle.
Thanks.
You need to specify the name of entity class when defining one-to-many, many-to-one relations. Still you are specifying Acme\AuthBundle\Entity\User not Aief\AuthBundle\Entity\User as targetEntity.
The same for Article and Article repository.

Categories