Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: - jpa-2.0

#Entity
public class Purveyor implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int ref_purveyor;
#Column(unique = true)
private String purveyor_name;
#Column(unique = true)
private int purveyor_number;
#Column(unique = true)
private String adress;
#Column(unique = true)
private int fax;
/**
*
* #return This returns the purveyor's ID.
*/
public int getRef_purveyor() {
return ref_purveyor;
}
/**
*
* #param ref_purveyor
* This refers to the ID of the new purveyor.
*/
public void setRef_purveyor(int ref_purveyor) {
this.ref_purveyor = ref_purveyor;
}
/**
*
* #return This return the name of the purveyor.
*/
public String getPurveyor_name() {
return purveyor_name;
}
/**
*
* #param purveyor_name
* This refers to the new purveyor's name.
*/
public void setPurveyor_name(String purveyor_name) {
this.purveyor_name = purveyor_name;
}
/**
*
* #return This returns the purveyor's number.
*/
public int getPurveyor_number() {
return purveyor_number;
}
/**
*
* #param purveyor_number
* This refers to the new purveyor's number.
*/
public void setPurveyor_number(int purveyor_number) {
this.purveyor_number = purveyor_number;
}
/**
*
* #return This returns purveyor's adress.
*/
public String getAdress() {
return adress;
}
/**
*
* #param adress
* This refers to the new purveyor's adress.
*/
public void setAdress(String adress) {
this.adress = adress;
}
/**
*
* #return This returns the purveyor's fax.
*/
public int getFax() {
return fax;
}
/**
*
* #param fax
* This refers to new purveyor's fax.
*/
public void setFax(int fax) {
this.fax = fax;
}
/*#OneToMany(mappedBy = "orders")
private Collection<Order> orders = new ArrayList<Order>();*/
#OneToMany(fetch=FetchType.LAZY,mappedBy = "deliveries")
private List<Delivery> deliveries = new ArrayList<Delivery>();
/**
*
* #see Order This refers to purveyor's Orders.
*/
/*public Collection<Order> getOrders() {
return orders;
}*/
/**
*
* #param orders
* This refers to the orders of the purveyor.
*/
/*public void setOrders(Collection<Order> orders) {
this.orders = orders;
}*/
/**
*
* #see Delivery This refers to purveyor's deliveries
*/
public List<Delivery> getDeliveries() {
return deliveries;
}
/**
*
* #param deliveries
* This refers to the deliveries of the purveyor.
*/
public void setDeliveries(List<Delivery> deliveries) {
this.deliveries = deliveries;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
======================-------------------------------==============================
#Entity
` public class Delivery implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "delivery_ref")
#GeneratedValue
private int delivery_ref;
#Column(unique = true)
private Date delivery_date;
#Column
private int quantity;
/**
* #return This returns the reference of the delivery.
*/
public int getDelivery_ref() {
return delivery_ref;
}
/**
*
* #param delivery_ref
* This refers to the new delivery.
*/
public void setDelivery_ref(int delivery_ref) {
this.delivery_ref = delivery_ref;
}
/**
* #return This returns the delivery date.
*/
public Date getDelivery_date() {
return delivery_date;
}
/**
*
* #param delivery_date
* This refers to the new date of delivery.
*/
public void setDelivery_date(Date delivery_date) {
this.delivery_date = delivery_date;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name = "ref_purveyor")
private Purveyor purveyor;
#OneToOne(mappedBy = "delivery", cascade = CascadeType.ALL)
private BillDelivery billD;
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public BillDelivery getBillD() {
return billD;
}
public void setBillD(BillDelivery billD) {
this.billD = billD;
}
public Purveyor getPurveyor() {
return purveyor;
}
public void setPurveyor(Purveyor purveyor) {
this.purveyor = purveyor;
}
}
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: x.Delivery.deliveries in x.Purveyor.deliveries
I tried the solutions already mentionned in this site but it doesn't work for me.
Can anybody helps me to resolve this issue

The message is pretty clear. You're saying to JPA: this OneToMany is the inverse side of the ManyToOne, defined in the Delivery entity, by the field deliveries. But there is no field deliveries in Delivery. The corresponding field in Delivery is named purveyor:
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name = "ref_purveyor")
private Purveyor purveyor;
^-- this is the other side of the association
So what you need is
#OneToMany(fetch=FetchType.LAZY,mappedBy = "puveyor")

Your mapping should look like this
Purveyor {
#OneToMany(fetch=FetchType.LAZY,mappedBy = "purveyor")
private List<Delivery> deliveries = new ArrayList<Delivery>();
}
Delivery {
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name = "ref_purveyor")
private Purveyor purveyor;
}

You need to assign the exact same value with:
-mappedBy value
-entity value

Related

Symfony Many to Many returns empty ArrayCollection

I have two entities Product and Part, connected by a many-to-many relationship.
class Part
{
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Product",
mappedBy="parts", fetch="EAGER")
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
class Product
{
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Part", inversedBy="products", cascade={"persist"})
* #ORM\JoinTable(name="products_parts",
* joinColumns={
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="part_id", referencedColumnName="id")
* }
* )
*/
private $parts;
public function __construct()
{
$this->parts = new ArrayCollection();
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getParts()
{
return $this->parts;
}
/**
* #param $parts
*/
public function setParts($parts)
{
$this->parts = $parts;
}
}
When I try get single Product and show all related parts. Symfony returns an empty array. Although the data is correctly added to the database. Where did I go wrong?

Class inherited from a JAX-WS generated class not getting marshalled

I have used wsimport to generate JAX-WS client code from WSDL.
I am invoking the web service with a parameter inherited from one of the classes generated by wsimport.
The class generated by wsimport is -
package com.mywebservice;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "MOR", propOrder = {
"value"
})
public class MOR {
#XmlValue
protected String value;
#XmlAttribute(name = "type")
protected String type;
/**
* Gets the value of the value property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets the value of the type property.
*
* #return
* possible object is
* {#link String }
*
*/
public String getType() {
return type;
}
/**
* Sets the value of the type property.
*
* #param value
* allowed object is
* {#link String }
*
*/
public void setType(String value) {
this.type = value;
}
public synchronized boolean equals(Object paramObject)
{
/*equals implementation*/
}
public synchronized int hashCode()
{
/*hashCode implementation*/*/
}
}
And I am passing instance of following class during web service invocation
public class TaggedMOR extends MOR {
private MOR moRef = null;
private String creatorMethodName = null;
public TaggedMOR(MOR MOR) {
moRef = MOR;
}
public TaggedMOR() {
}
public void setCreatorMethodName(String methodName) {
creatorMethodName = methodName;
}
public String getCreatorMethodName() {
return creatorMethodName;
}
public MOR getMoRef() {
return moRef;
}
}
I enabled http transport dump and saw that TaggedMOR was not marshalled in the SOAP envelope.
I also created an XMLJavaTypeAdapter but that also did not work.
Please advice, I am new to this.

Constraints like #Assert\NotBlank() annotation for entities does not work in unit test which gives semantical or auto loaded error

When I run the Unit test it gives the following error for the constraints
[Semantical Error] The annotation "#Symfony\Component\Validator\Constraints\NotBlank" in property AppBundle\Entity\User::$username does not exist, or could not be auto-loaded.
Here is my entity
User.class
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* Class User
* #package AppBundle\Entity
*
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* #ORM\Table(name="user")
* #ORM\HasLifecycleCallbacks()
*
*/
class User implements AdvancedUserInterface, \Serializable
{
/** #var double
* #ORM\Column(type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", length=60, unique=true)
* #Assert\NotBlank()
* #Assert\Email()
*/
private $username;
/**
* #ORM\Column(type="string", length=64)
*/
private $password;
/**
* #Assert\NotBlank()
* #Assert\Length(max=4096)
*/
private $plainPassword;
/**
* #var string
* #ORM\Column(name="fullname", type="string", length=100, nullable=false)
* #Assert\NotBlank()
*/
protected $fullname;
/**
* #var \DateTime
* #ORM\Column(name="createdat", type="datetime", nullable=true)
*/
protected $createdat;
/**
* #var \DateTime
* #ORM\Column(name="modifiedat", type="datetime", nullable=true)
*/
protected $modifiedat;
/**
* #ORM\Column(type="boolean", options={"default" = 1}, nullable=false)
*/
private $isactive = true;
public function __construct()
{
$this->updatedTimestamps();
}
/**
* Now we tell doctrine that before we persist or update we call the updatedTimestamps() function.
*
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setModifiedat(new \DateTime(date('Y-m-d H:i:s')));
if($this->getCreatedat() == null)
{
$this->setCreatedat(new \DateTime(date('Y-m-d H:i:s')));
}
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return '';
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isactive;
}
/** #see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->isactive
));
}
/** #see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->isactive
) = unserialize($serialized);
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* #param string $username
*
* #return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Set password
*
* #param string $password
*
* #return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Set isactive
*
* #param boolean $isactive
*
* #return User
*/
public function setIsactive($isactive)
{
$this->isactive = $isactive;
return $this;
}
/**
* Get isactive
*
* #return boolean
*/
public function getIsactive()
{
return $this->isactive;
}
/**
* #return mixed
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* #param mixed $plainPassword
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
}
/**
* Set fullname
*
* #param string $fullname
*
* #return User
*/
public function setFullname($fullname)
{
$this->fullname = $fullname;
return $this;
}
/**
* Get fullname
*
* #return string
*/
public function getFullname()
{
return $this->fullname;
}
/**
* Set createdat
*
* #param \DateTime $createdat
*
* #return User
*/
public function setCreatedat($createdat)
{
$this->createdat = $createdat;
return $this;
}
/**
* Get createdat
*
* #return \DateTime
*/
public function getCreatedat()
{
return $this->createdat;
}
/**
* Set modifiedat
*
* #param \DateTime $modifiedat
*
* #return User
*/
public function setModifiedat($modifiedat)
{
$this->modifiedat = $modifiedat;
return $this;
}
/**
* Get modifiedat
*
* #return \DateTime
*/
public function getModifiedat()
{
return $this->modifiedat;
}
/**
* The __toString method allows a class to decide how it will react when it is converted to a string.
*
* #return string
* #link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
*/
function __toString()
{
return "id: ". $this->id ." email: ". $this->username . " fullname: ". $this->fullname . " isactive: ". $this->isactive .
" createdat: ". $this->createdat->format('Y-m-d H:i:s') ." updatedat: ". $this->modifiedat->format('Y-m-d H:i:s');
}
}
This is my Unit Test classes:
TestBase.class
namespace tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\DependencyInjection\Container;
use Doctrine\Common\Annotations\AnnotationRegistry;
use AppKernel;
AnnotationRegistry::registerFile("../../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
require_once __DIR__ . "/../../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php";
class TestBase extends \PHPUnit_Framework_TestCase
{
/**
* #var Application
*/
protected $application;
/**
* #var \Doctrine\ORM\EntityManager
*/
protected $entityManager;
/**
* #var \AppKernel
*/
protected $kernel;
/**
* #var Container
*/
protected $container;
protected function setUp()
{
$this->kernel = new AppKernel("test", true);
$this->kernel->boot();
$this->application = new Application($this->kernel);
$this->application->setAutoExit(false);
// Store the container and the entity manager in test case properties
$this->container = $this->kernel->getContainer();
$this->entityManager = $this->container->get("doctrine")->getManager();
if(is_null($this->entityManager)){
print("upssss entitiy manager is null :(");
}
parent::setUp();
}
public function tearDown()
{
// Shutdown the kernel.
$this->kernel->shutdown();
parent::tearDown();
}
}
And here I test my User class just printing the database..
UserTest.class
namespace tests\AppBundle\Controller;
require_once ("TestBase.php");
class UserTest extends TestBase
{
protected function setUp()
{
parent::setUp();
}
public function tearDown()
{
parent::tearDown();
}
//generic method to list the users
public function listUsers($users ){
echo EOL, EOL;
foreach($users as $user){
echo $user, EOL;
}
echo EOL, EOL;
}
public function testListUsers(){
$users = $this->entityManager->getRepository('AppBundle:User')->findAll();
$this->assertGreaterThan(1, count($users));
$this->listUsers($users);
}
}
By the way it works when I don't use #Assert\NotBlank()
So from code-wise there is no problem at all... I guess it is just about autoloading when unit testing..
I really got stuck for 2 weeks with that..
Ok I found the answer..
When I changed test to dev in AppKernel it worked
old one was this:
$this->kernel = new AppKernel("test", true);
solution
$this->kernel = new AppKernel("dev", true);
I didn't change my AppKernel.php though
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
new Liuggio\ExcelBundle\LiuggioExcelBundle(),
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
];
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function getRootDir()
{
return __DIR__;
}
public function getCacheDir()
{
return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
}
public function getLogDir()
{
return dirname(__DIR__).'/var/logs';
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
}

Doctrine Orm - multiple OneToMany joins to the same table

I am struggling with the following. I am writing a basic User / Order relationship where a number of users play different roles. Each order is therefore linked to a user who is the initiator of the order, the courier (the user who is gonna pick up the order), the recipient, the user who is going to receive the order.
It stands that the Order table will have a number of relationships with the User table. Each Order will have a single user for each group, while each user can potentially have multiple relationships to the various Orders.
Unfortunately, I keep getting this error and I am not sure how to get around it:
An exception occurred while executing 'SELECT t0.id AS id1, t0.size AS
size2, t0.total AS total3, t0.initial_offer AS initial_offer4,
t0.user_order_owner AS user_order_owner5, t0.user_order_to AS
user_order_to6, t0.user_order_from AS user_order_from7,
t0.user_order_courier AS user_order_courier8, t0.event_collection_id
AS event_collection_id9, t0.event_delivery_id AS event_delivery_id10,
t0.currency_id AS currency_id11, t0.capacity_limitation_id AS
capacity_limitation_id12, t0.negotiation_id AS negotiation_id13,
t0.address_from_id AS address_from_id14, t0.address_to_id AS
address_to_id15 FROM order t0 WHERE t0.user_order_courier = ?' with
params [1]:
OR:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'order t0 WHERE
t0.user_order_courier = '1'' at line 1
Each Order in the system will have a user who is a:
Courier
Recipient
Sender
I am bug testing with the orderCourier join and joins are as follows:
Order Entity::
/**
* #ORM\ManyToOne(targetEntity="RoleBasedUser\Entity\User", inversedBy="orderCourier")
* #ORM\JoinColumn(name="user_order_courier", referencedColumnName="id")
*/
private $userCourier;
User Entity
/**
* #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userCourier")
*/
private $orderCourier;
The full entities are as follows:
My User Entity:
<?php
namespace RoleBasedUser\Entity;
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Gedmo\Mapping\Annotation as Gedmo;
use Rbac\Role\RoleInterface;
use ZfcRbac\Identity\IdentityInterface;
/**
* Class User
* #package RoleBasedUser\Entity
*
* #ORM\Entity(repositoryClass="UserRepository")
* #ORM\Table(name="rbu_users")
*/
class User implements IdentityInterface
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(name="id", type="integer", nullable=false)
*
* #var int
* #access protected
*/
private $id;
/**
* #ORM\Column(type="string", length=32, nullable=false, name="uuid")
*
* #var string
* #access private
*/
private $uuid;
/**
* #ORM\Column(type="string", unique=true, length=255, nullable=true, name="email")
*
* #var string
* #access private
*/
private $email;
/**
* #ORM\Column(type="smallint", nullable=true, name="state")
*
* #var integer
* #access private
*/
private $state;
/**
* #ORM\OneToMany(targetEntity="RoleBasedUser\Entity\LocationHistory", mappedBy="user")
*/
private $location;
/**
* #ORM\Column(nullable=true)
*/
private $parent;
/**
* #ORM\Column(nullable=true)
*/
private $firstName;
/**
* #ORM\Column(nullable=true)
*/
private $lastName;
/**
* #ORM\Column(nullable=true)
*/
private $password;
// /**
// * #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userOwner")
// */
// private $orderOwner;
//
// /** `
// * #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userTo")
// */
// private $orderTo;
//
// /**
// * #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userFrom")
// */
// private $orderFrom;
//
/**
* #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userCourier")
*/
private $orderCourier;
/**
* #ORM\OneToMany(targetEntity="RoleBasedUser\Entity\Feedback", mappedBy="client")
*/
private $clientFeedback;
/**
* #ORM\OneToMany(targetEntity="RoleBasedUser\Entity\Feedback", mappedBy="courier")
*/
private $feedbackCourier;
/**
* #ORM\OneToMany(targetEntity="RoleBasedUser\Entity\UserAvailability", mappedBy="user")
*/
private $pleaseUseMe;
/**
* #ORM\OneToMany(targetEntity="RoleBasedUser\Entity\UserStoredLocations", mappedBy="user")
*/
private $userAddresses;
/**
* #ORM\ManyToOne(targetEntity="RoleBasedUser\Entity\Corporate", inversedBy="user")
* #ORM\JoinColumn(name="corporate_id", referencedColumnName="id")
*/
private $corporate;
/**
* #ORM\OneToMany(targetEntity="Negotiation\Entity\Offer", mappedBy="user")
*/
private $offer;
/**
* #ORM\ManyToMany(targetEntity="HierarchicalRole")
* #ORM\JoinTable(name="rbu_users_roles",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*
* #var Collection
* #access private
*/
private $roles;
/**
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime")
*
* #var \DateTime
* #access protected
*/
protected $created;
/**
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*
* #var \DateTime
* #access protected
*/
protected $modified;
/**
* Initiates all ArrayCollections
*
* #access public
*/
public function __construct()
{
$this->roles = new ArrayCollection();
$this->orderOwner = new ArrayCollection();
$this->orderTo = new ArrayCollection();
$this->orderFrom = new ArrayCollection();
$this->orderCourier = new ArrayCollection();
$this->clientFeedback = new ArrayCollection();
$this->feedbackCourier = new ArrayCollection();
$this->pleaseUseMe = new ArrayCollection();
$this->userAddresses = new ArrayCollection();
$this->offer = new ArrayCollection();
}
/**
* {#inheritDoc}
*/
public function getRoles()
{
return $this->roles->toArray();
}
public function getRole()
{
if(!empty($this->getRoles()[0])) {
$roleObject = $this->getRoles()[0];
return $roleObject->getName();
} else return "Not set";
}
/**
* Set the list of roles
* #param Collection $roles
*/
public function setRoles(Collection $roles)
{
$this->roles->clear();
foreach ($roles as $role) {
$this->roles[] = $role;
}
}
/**
* Add one role to roles list
* #param \Rbac\Role\RoleInterface $role
*/
public function addRole(RoleInterface $role)
{
$this->roles[] = $role;
}
/**
* Add Roles to the collection
* #param Collection $roles
*/
public function addRoles(Collection $roles)
{
foreach($roles as $role) {
$this->roles->add($role);
}
}
/**
* Remove Roles from the collection
* #param Collection $roles
*/
public function removeRoles(Collection $roles)
{
foreach($roles as $role) {
$this->roles->removeElement($role);
}
}
/**
* #return mixed
*/
public function getCorporate()
{
return $this->corporate;
}
/**
* #param mixed $corporate
*/
public function setCorporate($corporate)
{
$this->corporate = $corporate;
}
/**
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* #param \DateTime $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* #return mixed
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* #param mixed $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getLastName()
{
return $this->lastName;
}
/**
* #param mixed $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
/**
* #return mixed
*/
public function getLocation()
{
return $this->location;
}
/**
* #param mixed $location
*/
public function setLocation($location)
{
$this->location = $location;
}
/**
* #return \DateTime
*/
public function getModified()
{
return $this->modified;
}
/**
* #param \DateTime $modified
*/
public function setModified($modified)
{
$this->modified = $modified;
}
/**
* #return mixed
*/
public function getParent()
{
return $this->parent;
}
/**
* #param mixed $parent
*/
public function setParent($parent)
{
$this->parent = $parent;
}
/**
* #return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* #param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* #param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* #return string
*/
public function getUuid()
{
return $this->uuid;
}
/**
* #param string $uuid
*/
public function setUuid($uuid)
{
$this->uuid = $uuid;
}
/**
* {#inheritDoc}
*/
public function getOrderOwner()
{
return $this->orderOwner->toArray();
}
/**
* #param Collection $orderOwner
*/
public function setOrderOwner(Collection $orderOwner)
{
$this->orderOwner->clear();
foreach ($orderOwner as $order) {
$this->orderOwner[] = $order;
}
}
/**
* #param Collection $orderOwner
*/
public function addOrderOwner(Collection $orderOwner)
{
foreach($orderOwner as $order) {
$this->orderOwner->add($order);
}
}
/**
* #param Collection $orderOwner
*/
public function removeOrderOwner(Collection $orderOwner)
{
foreach($orderOwner as $order) {
$this->orderOwner->removeElement($order);
}
}
/**
* {#inheritDoc}
*/
public function getOrderTo()
{
return $this->orderTo->toArray();
}
/**
* #param Collection $orderTo
*/
public function setOrderTo(Collection $orderTo)
{
$this->orderTo->clear();
foreach ($orderTo as $order) {
$this->orderTo[] = $order;
}
}
/**
* #param Collection $orderTo
*/
public function addOrderTo(Collection $orderTo)
{
foreach($orderTo as $order) {
$this->orderTo->add($order);
}
}
/**
* #param Collection $orderTo
*/
public function removeOrderTo(Collection $orderTo)
{
foreach($orderTo as $order) {
$this->orderTo->removeElement($order);
}
}
/**
* {#inheritDoc}
*/
public function getOrderFrom()
{
return $this->orderFrom->toArray();
}
/**
* #param Collection $orderFrom
*/
public function setOrderFrom(Collection $orderFrom)
{
$this->orderFrom->clear();
foreach ($orderFrom as $order) {
$this->orderFrom[] = $order;
}
}
/**
* #param Collection $orderFrom
*/
public function addOrderFrom(Collection $orderFrom)
{
foreach($orderFrom as $order) {
$this->orderFrom->add($order);
}
}
/**
* #param Collection $orderFrom
*/
public function removeOrderFrom(Collection $orderFrom)
{
foreach($orderFrom as $order) {
$this->orderFrom->removeElement($order);
}
}
/**
* {#inheritDoc}
*/
public function getOrderCourier()
{
return $this->orderCourier->toArray();
}
/**
* #param Collection $orderCourier
*/
public function setOrderCourier(Collection $orderCourier)
{
$this->orderCourier->clear();
foreach ($orderCourier as $order) {
$this->orderCourier[] = $order;
}
}
/**
* #param Collection $orderCourier
*/
public function addOrderCourier(Collection $orderCourier)
{
foreach($orderCourier as $order) {
$this->orderCourier->add($order);
}
}
/**
* #param Collection $orderCourier
*/
public function removeOrderCourier(Collection $orderCourier)
{
foreach($orderCourier as $order) {
$this->orderCourier->removeElement($order);
}
}
/**
* {#inheritDoc}
*/
public function getClientFeedback()
{
return $this->clientFeedback->toArray();
}
/**
* #param Collection $clientFeedback
*/
public function setClientFeedback(Collection $clientFeedback)
{
$this->clientFeedback->clear();
foreach ($clientFeedback as $feedback) {
$this->clientFeedback[] = $feedback;
}
}
/**
* #param Collection $clientFeedback
*/
public function addClientFeedback(Collection $clientFeedback)
{
foreach($clientFeedback as $feedback) {
$this->clientFeedback->add($feedback);
}
}
/**
* #param Collection $clientFeedback
*/
public function removeClientFeedback(Collection $clientFeedback)
{
foreach($clientFeedback as $feedback) {
$this->clientFeedback->removeElement($feedback);
}
}
/**
* {#inheritDoc}
*/
public function getFeedbackCourier()
{
return $this->feedbackCourier->toArray();
}
/**
* #param Collection $feedbackCourier
*/
public function setFeedbackCourier(Collection $feedbackCourier)
{
$this->feedbackCourier->clear();
foreach ($feedbackCourier as $feedback) {
$this->feedbackCourier[] = $feedback;
}
}
/**
* #param Collection $feedbackCourier
*/
public function addFeedbackCourier(Collection $feedbackCourier)
{
foreach($feedbackCourier as $feedback) {
$this->feedbackCourier->add($feedback);
}
}
/**
* #param Collection $feedbackCourier
*/
public function removeFeedbackCourier(Collection $feedbackCourier)
{
foreach($feedbackCourier as $feedback) {
$this->feedbackCourier->removeElement($feedback);
}
}
/**
* {#inheritDoc}
*/
public function getPleaseUseMe()
{
return $this->pleaseUseMe->toArray();
}
/**
* #param Collection $pleaseUseMe
*/
public function setPleaseUseMe(Collection $pleaseUseMe)
{
$this->pleaseUseMe->clear();
foreach ($pleaseUseMe as $useme) {
$this->pleaseUseMe[] = $useme;
}
}
/**
* #param Collection $pleaseUseMe
*/
public function addPleaseUseMe(Collection $pleaseUseMe)
{
foreach($pleaseUseMe as $useme) {
$this->pleaseUseMe->add($useme);
}
}
/**
* #param Collection $pleaseUseMe
*/
public function removePleaseUseMe(Collection $pleaseUseMe)
{
foreach($pleaseUseMe as $useme) {
$this->pleaseUseMe->removeElement($useme);
}
}
/**
* {#inheritDoc}
*/
public function getUserAddresses()
{
return $this->userAddresses->toArray();
}
/**
* #param Collection $userAddresses
*/
public function setUserAddresses(Collection $userAddresses)
{
$this->userAddresses->clear();
foreach ($userAddresses as $address) {
$this->userAddresses[] = $address;
}
}
/**
* #param Collection $userAddresses
*/
public function addUserAddresses(Collection $userAddresses)
{
foreach($userAddresses as $address) {
$this->userAddresses->add($address);
}
}
/**
* #param Collection $userAddresses
*/
public function removeUserAddresses(Collection $userAddresses)
{
foreach($userAddresses as $address) {
$this->userAddresses->removeElement($address);
}
}
/**
* {#inheritDoc}
*/
public function getOffer()
{
return $this->offer->toArray();
}
/**
* #param Collection $offer
*/
public function setOffer(Collection $offer)
{
$this->offer->clear();
foreach ($offer as $n) {
$this->offer[] = $n;
}
}
/**
* #param Collection $offer
*/
public function addOffer(Collection $offer)
{
foreach($offer as $n) {
$this->offer->add($n);
}
}
/**
* #param Collection $offer
*/
public function removeOffer(Collection $offer)
{
foreach($offer as $n) {
$this->offer->removeElement($n);
}
}
/**
* #param int $state
*/
public function setState($state)
{
$this->state = $state;
}
/**
* #return int
*/
public function getState()
{
return $this->state;
}
}
And my Order Entity:
<?php
namespace Negotiation\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class Order
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $size;
/**
* #ORM\Column(type="float", length=15, nullable=true)
*/
private $total;
/**
* #ORM\Column(nullable=true)
*/
private $initialOffer;
/**
* #ORM\ManyToOne(targetEntity="RoleBasedUser\Entity\User", inversedBy="orderOwner")
* #ORM\JoinColumn(name="user_order_owner", referencedColumnName="id")
*/
private $userOwner;
/**
* #ORM\ManyToOne(targetEntity="RoleBasedUser\Entity\User", inversedBy="orderTo")
* #ORM\JoinColumn(name="user_order_to", referencedColumnName="id")
*/
private $userTo;
/**
* #ORM\OneToMany(targetEntity="RoleBasedUser\Entity\Feedback", mappedBy="order")
*/
private $feedback;
/**
* #ORM\ManyToOne(targetEntity="RoleBasedUser\Entity\User", inversedBy="orderFrom")
* #ORM\JoinColumn(name="user_order_from", referencedColumnName="id")
*/
private $userFrom;
/**
* #ORM\ManyToOne(targetEntity="RoleBasedUser\Entity\User", inversedBy="orderCourier")
* #ORM\JoinColumn(name="user_order_courier", referencedColumnName="id")
*/
private $userCourier;
/**
* #ORM\ManyToOne(targetEntity="Negotiation\Entity\Event", inversedBy="wayBillCollection")
*/
private $eventCollection;
/**
* #ORM\ManyToOne(targetEntity="Negotiation\Entity\Event", inversedBy="wayBillDelivery")
*/
private $eventDelivery;
/**
* #ORM\ManyToOne(targetEntity="Negotiation\Entity\Currency", inversedBy="order")
*/
private $currency;
/**
* #ORM\ManyToOne(targetEntity="Negotiation\Entity\CapacityLimitation", inversedBy="order")
*/
private $capacityLimitation;
/**
* #ORM\ManyToOne(targetEntity="Negotiation\Entity\Negotiation", inversedBy="order")
*/
private $negotiation;
/**
* #ORM\ManyToOne(targetEntity="Negotiation\Entity\Address", inversedBy="orderFrom")
*/
private $addressFrom;
/**
* #ORM\ManyToOne(targetEntity="Negotiation\Entity\Address", inversedBy="orderTo")
*/
private $addressTo;
/**
* #return mixed
*/
public function getAddressFrom()
{
return $this->addressFrom;
}
/**
* #param mixed $addressFrom
*/
public function setAddressFrom($addressFrom)
{
$this->addressFrom = $addressFrom;
}
/**
* #return mixed
*/
public function getAddressTo()
{
return $this->addressTo;
}
/**
* #param mixed $addressTo
*/
public function setAddressTo($addressTo)
{
$this->addressTo = $addressTo;
}
/**
* #return mixed
*/
public function getCapacityLimitation()
{
return $this->capacityLimitation;
}
/**
* #param mixed $capacityLimitation
*/
public function setCapacityLimitation($capacityLimitation)
{
$this->capacityLimitation = $capacityLimitation;
}
/**
* #return mixed
*/
public function getCurrency()
{
return $this->currency;
}
/**
* #param mixed $currency
*/
public function setCurrency($currency)
{
$this->currency = $currency;
}
/**
* #return mixed
*/
public function getEventCollection()
{
return $this->eventCollection;
}
/**
* #param mixed $eventCollection
*/
public function setEventCollection($eventCollection)
{
$this->eventCollection = $eventCollection;
}
/**
* #return mixed
*/
public function getEventDelivery()
{
return $this->eventDelivery;
}
/**
* #param mixed $eventDelivery
*/
public function setEventDelivery($eventDelivery)
{
$this->eventDelivery = $eventDelivery;
}
/**
* #return mixed
*/
public function getFeedback()
{
return $this->feedback;
}
/**
* #param mixed $feedback
*/
public function setFeedback($feedback)
{
$this->feedback = $feedback;
}
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getInitialOffer()
{
return $this->initialOffer;
}
/**
* #param mixed $initialOffer
*/
public function setInitialOffer($initialOffer)
{
$this->initialOffer = $initialOffer;
}
/**
* #return mixed
*/
public function getNegotiation()
{
return $this->negotiation;
}
/**
* #param mixed $negotiation
*/
public function setNegotiation($negotiation)
{
$this->negotiation = $negotiation;
}
/**
* #return mixed
*/
public function getSize()
{
return $this->size;
}
/**
* #param mixed $size
*/
public function setSize($size)
{
$this->size = $size;
}
/**
* #return mixed
*/
public function getTotal()
{
return $this->total;
}
/**
* #param mixed $total
*/
public function setTotal($total)
{
$this->total = $total;
}
/**
* #return mixed
*/
public function getUserCourier()
{
return $this->userCourier;
}
/**
* #param mixed $userCourier
*/
public function setUserCourier($userCourier)
{
$this->userCourier = $userCourier;
}
/**
* #return mixed
*/
public function getUserFrom()
{
return $this->userFrom;
}
/**
* #param mixed $userFrom
*/
public function setUserFrom($userFrom)
{
$this->userFrom = $userFrom;
}
/**
* #return mixed
*/
public function getUserOwner()
{
return $this->userOwner;
}
/**
* #param mixed $userOwner
*/
public function setUserOwner($userOwner)
{
$this->userOwner = $userOwner;
}
/**
* #return mixed
*/
public function getUserTo()
{
return $this->userTo;
}
/**
* #param mixed $userTo
*/
public function setUserTo($userTo)
{
$this->userTo = $userTo;
}
}
EDIT:
The errors are appearing when I attempt to edit the user and the exceptions occur on: $this->updateForm->bind($object);
$user_id = (int) $this->params()->fromRoute('user_id', 0);
$object = $this->userService->find($user_id);
$prg = $this->prg();
if ($prg instanceof Response) {
return $prg;
} elseif ($prg === false) {
$this->updateForm->bind($object);
return $this->getVM()->setVariables([
'user' => $object,
'form' => $this->updateForm
]);
}
EDIT:
Essentially these three tables cause the problem, as soon as I remove them , all works as expected:
// /**
// * #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userOwner")
// */
// private $orderOwner;
// /** `
// * #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userTo")
// */
// private $orderTo;
// /**
// * #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userFrom")
// */
// private $orderFrom;
// /**
// * #ORM\OneToMany(targetEntity="Negotiation\Entity\Order", mappedBy="userCourier")
// */
// private $orderCourier;
The solution to this problem was to sort out my getters and setters. I was too focused on the annotations of the problem that I never looked towards them for a possible error.
Thanks to Svengali who got me to re-look at what I was doing wrong.
My Getters/Setters where originally like this:
/**
* {#inheritDoc}
*/
public function getOrderCourier()
{
return $this->orderCourier->toArray();
}
/**
* #param Collection $orderCourier
*/
public function setOrderCourier(Collection $orderCourier)
{
$this->orderCourier->clear();
foreach ($orderCourier as $order) {
$this->orderCourier[] = $order;
}
}
/**
* #param Collection $orderCourier
*/
public function addOrderCourier(Collection $orderCourier)
{
foreach($orderCourier as $order) {
$this->orderCourier->add($order);
}
}
/**
* #param Collection $orderCourier
*/
public function removeOrderCourier(Collection $orderCourier)
{
foreach($orderCourier as $order) {
$this->orderCourier->removeElement($order);
}
}
I changed them to:
/**
* #return mixed
*/
public function getOrderCourier()
{
return $this->orderCourier;
}
/**
* #param mixed $orderCourier
*/
public function setOrderCourier($orderCourier)
{
$this->orderCourier = $orderCourier;
}
And the problem no longer persists.
Thanks for the input, although it may have only been a simple question, often this is all a developer needs in order to push them in the right direction.

must return a string value in zend2. How?

I have a problem with a zend2 form. I made an entity which gets some data from the database and joins some tables...
here is the entity:
class Campaigns
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
protected $id;
/**
*
* #ORM\Column(name="campaign_name", type="string")
*
*/
protected $campaigns;
/**
* #var mixed
*
* #ORM\ManyToMany(targetEntity="Application\Entity\Countries", cascade={"persist"}, orphanRemoval=false)
* #ORM\JoinTable(name="campaigns_countries",
* joinColumns={#ORM\JoinColumn(name="campaign_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={#ORM\JoinColumn(name="country_id", referencedColumnName="id", onDelete="CASCADE")}
* )
*/
protected $countries;
Below this code are the getters and setters, a construct function, an add and an remove function.
Here they are:
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getCampaigns()
{
return $this->campaigns;
}
public function setCampaigns($campaigns)
{
$this->campaigns = $campaigns;
return $this;
}
public function addCampaigns($campaigns = null)
{
foreach ($campaigns as $c) {
if (!$this->campaigns->contains($c)) {
$this->campaigns->add($c);
}
}
}
public function removeCampaigns($campaigns)
{
foreach ($campaigns as $c) {
if ($this->campaigns->contains($c)) {
$this->campaigns->removeElement($c);
}
}
}
public function getCountries()
{
return $this->countries;
}
public function setCountries($countries)
{
$this->countries = $countries;
return $this;
}
public function addCountries($countries = null)
{
foreach ($countries as $c) {
if (!$this->countries->contains($c)) {
$this->countries->add($c);
}
}
}
public function removeCountries($countries)
{
foreach ($countries as $c) {
if ($this->countries->contains($c)) {
$this->countries->removeElement($c);
}
}
} //construct for countries
public function __construct()
{
$this->setCountries(new ArrayCollection());
}
My problem is with the protected $countries. If i add in the form the property value, it gives me the "countries" property not found in entity.
If I do not add it, and instead use __toString() function, it gives me an error saying that it could not convert countries to string...in the __toString() function I added the following code:
public function __toString()
{
return $this->countries;
}
Thank you for all your help!
AE
You say you want a string containing all related countries. The following code demonstrates how you could achieve this:
$campaignCountryNames = array();
$campaignCountries = $campaign->getCountries();
foreach ($campaignCountries as $country) {
// I assume your Country entity has a name property
$campaignCountryNames[] = $country->getName();
}
echo implode(', ', $campaignCountryNames);