I have multiple users, with multiple stores in a many to many relational database. Every user has multiple stores attached to them.
Now, i want to load all the storenames from a logged in user in a select form.
How can i do this?
My user entity:
namespace Application\Entity;
use BjyAuthorize\Provider\Role\ProviderInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ZfcUser\Entity\UserInterface;
/**
* An example of how to implement a role aware user entity.
*
* #ORM\Entity
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="Application\Repositories\UserRepository")
*
*/
class User implements UserInterface, ProviderInterface
{
/**
* #var int
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", length=255, unique=true, nullable=true)
*/
protected $username;
/**
* #var string
* #ORM\Column(type="string", unique=true, length=255)
*/
protected $email;
/**
* #var string
* #ORM\Column(type="string", length=50, nullable=true)
*/
protected $displayName;
/**
* #var string
* #ORM\Column(type="string", length=128)
*/
protected $password;
/**
* #var int
*/
protected $state;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\Role")
* #ORM\JoinTable(name="user_role_linker",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
protected $roles;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\Store")
* #ORM\JoinTable(name="user_store_linker",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="store_id", referencedColumnName="id")}
* )
*/
protected $stores;
/**
* Initialies the roles variable.
*/
public function __construct()
{
$this->roles = new ArrayCollection();
$this->stores = new ArrayCollection();
}
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param int $id
*
* #return void
*/
public function setId($id)
{
$this->id = (int) $id;
}
/**
* Get username.
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set username.
*
* #param string $username
*
* #return void
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Get email.
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email.
*
* #param string $email
*
* #return void
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* Set displayName.
*
* #param string $displayName
*
* #return void
*/
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
}
/**
* Get password.
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set password.
*
* #param string $password
*
* #return void
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Get state.
*
* #return int
*/
public function getState()
{
return $this->state;
}
/**
* Set state.
*
* #param int $state
*
* #return void
*/
public function setState($state)
{
$this->state = $state;
}
/**
* Get role.
*
* #return array
*/
public function getRoles()
{
return $this->roles->getValues();
}
/**
* Add a role to the user.
*
* #param Role $role
*
* #return void
*/
public function addRole($role)
{
$this->roles[] = $role;
}
/**
* Get store.
*
* #return array
*/
public function getStores()
{
return $this->stores;
}
/**
* Get store.
*
* #return array
*/
public function getStore($id)
{
return $this->stores[$id]->getValues();
}
/**
* Add a store to the user.
*
* #param Role $store
*
* #return void
*/
public function addStore($store)
{
$this->stores[] = $store;
}
}
My store Entity:
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* An example entity that represents a store.
*
* #ORM\Entity
* #ORM\Table(name="store")
*
*/
class Store
{
/**
* #var int
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", name="storeName", length=255, unique=true, nullable=true)
*/
protected $storeName;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\Product" )
*/
protected $products;
/**
* Initialies the roles variable.
*/
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get the id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set the id.
*
* #param int $id
*
* #return void
*/
public function setId($id)
{
$this->id = (int)$id;
}
/**
* Get the store id.
*
* #return string
*/
public function getStoreName()
{
return $this->storeName;
}
/**
* Set the store id.
*
* #param string $storeName
*
* #return void
*/
public function setStoreName($storeName)
{
$this->storeName = (string) $storeName;
}
/**
* Get product.
*
* #return array
*/
public function getProducts()
{
return $this->products->getValues();
}
/**
* Add a product to the user.
*
* #param Role $product
*
* #return void
*/
public function addProduct($products)
{
$this->products[] = $products;
}
}
My form:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'stores',
'attributes' => array(
'multiple' => true,
),
'options' => array(
'object_manager' => $objectManager,
'target_class' => 'Application\Entity\User',
'label' => 'Selecteer winkel',
'column-size' => 'sm-9',
'label_attributes' => array('class' => 'col-sm-3 control-label'),
'property' => 'stores',
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('id' => $userid)
),
),
'is_method' => true,
),
));
I'm getting this message:
File:
zend/vendor/zendframework/zendframework/library/Zend/View/Helper/Escaper/AbstractHelper.php:70
Message:
Object provided to Escape helper, but flags do not allow recursion
Anyone?
Ok i've figured it out with a custom repository.
Add a repositoryclass to your store entity
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* #ORM\Entity
* #ORM\Table(name="store")
* #ORM\Entity(repositoryClass="Application\Repositories\StoreRepository")
*
*/
class Store
{
/**
* #var int
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", name="storeName", length=255, unique=true, nullable=true)
*/
protected $storeName;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\Product", inversedBy="stores")
* #ORM\JoinTable(name="product_store")
*/
protected $products;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\Category", inversedBy="stores")
* #ORM\JoinTable(name="category_store")
*/
protected $categories;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\User", inversedBy="stores")
* #ORM\JoinTable(name="user_store")
*/
protected $users;
/**
* Initialies the roles variable.
*/
public function __construct()
{
$this->products = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->users = new ArrayCollection();
}
/**
* Get the id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set the id.
*
* #param int $id
*
* #return void
*/
public function setId($id)
{
$this->id = (int)$id;
}
/**
* Get the store id.
*
* #return string
*/
public function getStoreName()
{
return $this->storeName;
}
/**
* Set the store id.
*
* #param string $storeName
*
* #return void
*/
public function setStoreName($storeName)
{
$this->storeName = (string) $storeName;
}
/**
* Get product.
*
* #return array
*/
public function getProducts()
{
return $this->products->getValues();
}
/**
* Add a product to the user.
*
* #param Role $product
*
* #return void
*/
public function addProduct($products)
{
$this->products[] = $products;
}
/**
* Get category.
*
* #return array
*/
public function getCategories()
{
return $this->categories->getValues();
}
/**
* Add a product to the user.
*
* #param Role $product
*
* #return void
*/
public function addCategory($categories)
{
$this->categories[] = $categories;
}
/**
* Get category.
*
* #return array
*/
public function getUsers()
{
return $this->users->getValues();
}
/**
* Add a product to the user.
*
* #param Role $product
*
* #return void
*/
public function addUser($users)
{
$this->users[] = $users;
}
}
My user entity
namespace Application\Entity;
use BjyAuthorize\Provider\Role\ProviderInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ZfcUser\Entity\UserInterface;
/**
*
* #ORM\Entity
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="Application\Repositories\UserRepository")
*
*/
class User implements UserInterface, ProviderInterface
{
/**
* #var int
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", length=255, unique=true, nullable=true)
*/
protected $username;
/**
* #var string
* #ORM\Column(type="string", unique=true, length=255)
*/
protected $email;
/**
* #var string
* #ORM\Column(type="string", length=50, nullable=true)
*/
protected $displayName;
/**
* #var string
* #ORM\Column(type="string", length=128)
*/
protected $password;
/**
* #var int
*/
protected $state;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\Role")
* #ORM\JoinTable(name="user_role_linker",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
protected $roles;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="Application\Entity\Store", mappedBy="users")
*/
protected $stores;
/**
* Initialies the roles variable.
*/
public function __construct()
{
$this->roles = new ArrayCollection();
$this->stores = new ArrayCollection();
}
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param int $id
*
* #return void
*/
public function setId($id)
{
$this->id = (int) $id;
}
/**
* Get username.
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set username.
*
* #param string $username
*
* #return void
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Get email.
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email.
*
* #param string $email
*
* #return void
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* Set displayName.
*
* #param string $displayName
*
* #return void
*/
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
}
/**
* Get password.
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set password.
*
* #param string $password
*
* #return void
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Get state.
*
* #return int
*/
public function getState()
{
return $this->state;
}
/**
* Set state.
*
* #param int $state
*
* #return void
*/
public function setState($state)
{
$this->state = $state;
}
/**
* Get role.
*
* #return array
*/
public function getRoles()
{
return $this->roles->getValues();
}
/**
* Add a role to the user.
*
* #param Role $role
*
* #return void
*/
public function addRole($role)
{
$this->roles[] = $role;
}
/**
* Get store.
*
* #return array
*/
public function getStores()
{
return $this->stores;
}
/**
* Get store.
*
* #return array
*/
public function getStore($id)
{
return $this->stores[$id]->getValues();
}
/**
* Add a store to the user.
*
* #param Role $store
*
* #return void
*/
public function addStore($store)
{
$this->stores[] = $store;
}
}
The object select
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'stores',
'attributes' => array(
'multiple' => true,
),
'options' => array(
'object_manager' => $objectManager,
'target_class' => 'Application\Entity\Store',
'label' => 'Selecteer winkel',
'column-size' => 'sm-9',
'label_attributes' => array('class' => 'col-sm-3 control-label'),
'property' => 'storeName',
'is_method' => true,
'find_method' => array(
'name' => 'storesByUser',
'params' => array(
'criteria' => array('id' => $userid),
),
),
),
));
My custom store repository
<?php
namespace Application\Repositories;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\DBAL\Types\Type;
class StoreRepository extends EntityRepository
{
public function storesByUser(array $criteria){
return $this->createQueryBuilder('s')
->select('s')
->innerJoin("s.users", "u", "WITH", "u=:userid")
->setParameter("userid", $criteria['id'])
->getQuery()->getResult();
}
}
Related
trying to fix this over 48 hours. I have an entity BookingRequest.php which content is:
<?php
namespace Kutiwa\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* BookingRequest
*
* #ORM\Table(name="booking_request")
* #ORM\Entity(repositoryClass="Kutiwa\PlatformBundle\Repository\BookingRequestRepository")
*/
class BookingRequest
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="code", type="string", length=255, unique=true)
*/
private $code;
/**
* #var string
*
* #ORM\Column(name="arrival", type="string", length=255)
*/
private $arrival;
/**
* #var string
*
* #ORM\Column(name="departure", type="string", length=255)
*/
private $departure;
/**
* #var string
*
* #ORM\Column(name="requestDate", type="string", length=255)
*/
private $requestDate;
/**
* #var int
*
* #ORM\Column(name="rooms", type="integer")
*/
private $rooms;
/**
* #var string
*
* #ORM\Column(name="minPricing", type="string", length=255)
*/
private $minPricing;
/**
* #var string
*
* #ORM\Column(name="maxPricing", type="string", length=255)
*/
private $maxPricing;
/**
* #var string
*
* #ORM\Column(name="customerName", type="string", length=255)
*/
private $customerName;
/**
* #var string
*
* #ORM\Column(name="customerEmail", type="string", length=255, nullable=true)
*/
private $customerEmail;
/**
* #var string
*
* #ORM\Column(name="customerPhone", type="string", length=255)
*/
private $customerPhone;
/**
* #ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\City")
* #ORM\JoinColumn(nullable=false)
*/
private $destinationCity;
/**
* #ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\City")
* #ORM\JoinColumn(nullable=false)
*/
private $customerCity;
/**
* #ORM\OneToOne(targetEntity="Kutiwa\PlatformBundle\Entity\MissionConfig", cascade={"persist"})
*/
private $missionConfig;
/**
* #ORM\OneToOne(targetEntity="Kutiwa\PlatformBundle\Entity\TourismConfig", cascade={"persist"})
*/
private $tourismConfig;
/**
* #ORM\OneToMany(targetEntity="Kutiwa\PlatformBundle\Entity\RoomsConfig", mappedBy="bookingRequest")
*/
private $roomsConfigs;
public function __construct() {
$this->setRequestDate(date("d/m/Y"));
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set code
*
* #param string $code
*
* #return BookingRequest
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* #return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set arrival
*
* #param string $arrival
*
* #return BookingRequest
*/
public function setArrival($arrival)
{
$this->arrival = $arrival;
return $this;
}
/**
* Get arrival
*
* #return string
*/
public function getArrival()
{
return $this->arrival;
}
/**
* Set departure
*
* #param string $departure
*
* #return BookingRequest
*/
public function setDeparture($departure)
{
$this->departure = $departure;
return $this;
}
/**
* Get departure
*
* #return string
*/
public function getDeparture()
{
return $this->departure;
}
/**
* Set requestDate
*
* #param string $requestDate
*
* #return BookingRequest
*/
public function setRequestDate($requestDate)
{
$this->requestDate = $requestDate;
return $this;
}
/**
* Get requestDate
*
* #return string
*/
public function getRequestDate()
{
return $this->requestDate;
}
/**
* Set rooms
*
* #param integer $rooms
*
* #return BookingRequest
*/
public function setRooms($rooms)
{
$this->rooms = $rooms;
return $this;
}
/**
* Get rooms
*
* #return integer
*/
public function getRooms()
{
return $this->rooms;
}
/**
* Set minPricing
*
* #param string $minPricing
*
* #return BookingRequest
*/
public function setMinPricing($minPricing)
{
$this->minPricing = $minPricing;
return $this;
}
/**
* Get minPricing
*
* #return string
*/
public function getMinPricing()
{
return $this->minPricing;
}
/**
* Set maxPricing
*
* #param string $maxPricing
*
* #return BookingRequest
*/
public function setMaxPricing($maxPricing)
{
$this->maxPricing = $maxPricing;
return $this;
}
/**
* Get maxPricing
*
* #return string
*/
public function getMaxPricing()
{
return $this->maxPricing;
}
/**
* Set customerName
*
* #param string $customerName
*
* #return BookingRequest
*/
public function setCustomerName($customerName)
{
$this->customerName = $customerName;
return $this;
}
/**
* Get customerName
*
* #return string
*/
public function getCustomerName()
{
return $this->customerName;
}
/**
* Set customerEmail
*
* #param string $customerEmail
*
* #return BookingRequest
*/
public function setCustomerEmail($customerEmail)
{
$this->customerEmail = $customerEmail;
return $this;
}
/**
* Get customerEmail
*
* #return string
*/
public function getCustomerEmail()
{
return $this->customerEmail;
}
/**
* Set customerPhone
*
* #param string $customerPhone
*
* #return BookingRequest
*/
public function setCustomerPhone($customerPhone)
{
$this->customerPhone = $customerPhone;
return $this;
}
/**
* Get customerPhone
*
* #return string
*/
public function getCustomerPhone()
{
return $this->customerPhone;
}
/**
* Set destinationCity
*
* #param \Kutiwa\PlatformBundle\Entity\City $destinationCity
*
* #return BookingRequest
*/
public function setDestinationCity(\Kutiwa\PlatformBundle\Entity\City $destinationCity)
{
$this->destinationCity = $destinationCity;
return $this;
}
/**
* Get destinationCity
*
* #return \Kutiwa\PlatformBundle\Entity\City
*/
public function getDestinationCity()
{
return $this->destinationCity;
}
/**
* Set customerCity
*
* #param \Kutiwa\PlatformBundle\Entity\City $customerCity
*
* #return BookingRequest
*/
public function setCustomerCity(\Kutiwa\PlatformBundle\Entity\City $customerCity)
{
$this->customerCity = $customerCity;
return $this;
}
/**
* Get customerCity
*
* #return \Kutiwa\PlatformBundle\Entity\City
*/
public function getCustomerCity()
{
return $this->customerCity;
}
/**
* Set missionConfig
*
* #param \Kutiwa\PlatformBundle\Entity\MissionConfig $missionConfig
*
* #return BookingRequest
*/
public function setMissionConfig(\Kutiwa\PlatformBundle\Entity\MissionConfig $missionConfig = null)
{
$this->missionConfig = $missionConfig;
return $this;
}
/**
* Get missionConfig
*
* #return \Kutiwa\PlatformBundle\Entity\MissionConfig
*/
public function getMissionConfig()
{
return $this->missionConfig;
}
/**
* Set tourismConfig
*
* #param \Kutiwa\PlatformBundle\Entity\TourismConfig $tourismConfig
*
* #return BookingRequest
*/
public function setTourismConfig(\Kutiwa\PlatformBundle\Entity\TourismConfig $tourismConfig = null)
{
$this->tourismConfig = $tourismConfig;
return $this;
}
/**
* Get tourismConfig
*
* #return \Kutiwa\PlatformBundle\Entity\TourismConfig
*/
public function getTourismConfig()
{
return $this->tourismConfig;
}
/**
* Add roomsConfig
*
* #param \Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig
*
* #return BookingRequest
*/
public function addRoomsConfig(\Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig)
{
$this->roomsConfigs[] = $roomsConfig;
return $this;
}
/**
* Remove roomsConfig
*
* #param \Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig
*/
public function removeRoomsConfig(\Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig)
{
$this->roomsConfigs->removeElement($roomsConfig);
}
/**
* Get roomsConfigs
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getRoomsConfigs()
{
return $this->roomsConfigs;
}
}
and another entity RoomsConfig.php
<?php
namespace Kutiwa\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RoomsConfig
*
* #ORM\Table(name="rooms_config")
* #ORM\Entity(repositoryClass="Kutiwa\PlatformBundle\Repository\RoomsConfigRepository")
*/
class RoomsConfig
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var bool
*
* #ORM\Column(name="airConditionner", type="boolean")
*/
private $airConditionner;
/**
* #var bool
*
* #ORM\Column(name="wifi", type="boolean")
*/
private $wifi;
/**
* #var bool
*
* #ORM\Column(name="balcony", type="boolean")
*/
private $balcony;
/**
* #var string
*
* #ORM\Column(name="tv", type="boolean")
*/
private $tv;
/**
* #ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\BookingRequest", inversedBy="roomsConfigs", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $bookingRequest;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set airConditionner
*
* #param boolean $airConditionner
*
* #return RoomsConfig
*/
public function setAirConditionner($airConditionner)
{
$this->airConditionner = $airConditionner;
return $this;
}
/**
* Get airConditionner
*
* #return boolean
*/
public function getAirConditionner()
{
return $this->airConditionner;
}
/**
* Set wifi
*
* #param boolean $wifi
*
* #return RoomsConfig
*/
public function setWifi($wifi)
{
$this->wifi = $wifi;
return $this;
}
/**
* Get wifi
*
* #return boolean
*/
public function getWifi()
{
return $this->wifi;
}
/**
* Set balcony
*
* #param boolean $balcony
*
* #return RoomsConfig
*/
public function setBalcony($balcony)
{
$this->balcony = $balcony;
return $this;
}
/**
* Get balcony
*
* #return boolean
*/
public function getBalcony()
{
return $this->balcony;
}
/**
* Set tv
*
* #param boolean $tv
*
* #return RoomsConfig
*/
public function setTv($tv)
{
$this->tv = $tv;
return $this;
}
/**
* Get tv
*
* #return boolean
*/
public function getTv()
{
return $this->tv;
}
/**
* Set bookingRequest
*
* #param \Kutiwa\PlatformBundle\Entity\BookingRequest $bookingRequest
*
* #return RoomsConfig
*/
public function setBookingRequest(\Kutiwa\PlatformBundle\Entity\BookingRequest $bookingRequest)
{
$this->bookingRequest = $bookingRequest;
$bookingRequest->addRoomsConfig($this);
return $this;
}
/**
* Get bookingRequest
*
* #return \Kutiwa\PlatformBundle\Entity\BookingRequest
*/
public function getBookingRequest()
{
return $this->bookingRequest;
}
}
The problem is, when I try to persist a RoomsConfig entity, I got an error
A new entity was found through the relationship
'Kutiwa\PlatformBundle\Entity\BookingRequest#destinationCity' that was
not configured to cascade persist operations for entity: TIKO. To
solve this issue: Either explicitly call EntityManager#persist() on
this unknown entity or configure cascade persist this association in
the mapping for example #ManyToOne(..,cascade={"persist"})
I don't need a cascade effect on City and BookingRequest relation, here is my BookingRequestType.php
<?php
namespace Kutiwa\PlatformBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BookingRequestType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('arrival', TextType::class)
->add('departure', TextType::class)
->add('rooms', IntegerType::class)
->add('customerName', TextType::class)
->add('customerEmail', TextType::class, array('required' => false))
->add('customerPhone', TextType::class)
->add('destinationCity', EntityType::class, array(
'choice_label' => 'name',
'class' => 'KutiwaPlatformBundle:City'
))
->add('customerCity', EntityType::class, array(
'choice_label' => 'name',
'class' => 'KutiwaPlatformBundle:City'
));
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Kutiwa\PlatformBundle\Entity\BookingRequest'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'kutiwa_platformbundle_bookingrequest';
}
}
If I try to persist only BookingRequest entity, there is no problem, but when I persist RoomsConfig entity, I got that error. Help please. Thks.
Before persist your RoomsConfig, try to use findOneBy to find BookingRequest, or whatever you use to find the entity, and then set the found entity with setBookingRequest.
Here's my thoughts and where I'm at. Please tell me if any of this is bad practice!
I have 3 Entities: Article, Author, Reviews:
Relationships:
Article has One Author.
Article has many Reviews.
Author has many Articles.
Review has One Author.
Review has One Article.
I've created a controller called reviewAction where I'm going to build a review form, pass it to a review view, and embed that on my article show view.
Here's my controller so far (it isn't working)
public function reviewAction(Request $request, Article $article)
{
$reviewForm = $this->createFormBuilder($article);
$reviewForm->add('review')->add('title')
->add('author', AuthorType::class, array("label" => FALSE))
->add('rating', ChoiceType::class, array(
'choices' => array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' =>'5'),
'expanded' => true,
'multiple' => false
))
->getForm();
if ($reviewForm->isSubmitted() && $reviewForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('article_show', array('id' => $article->getId()));
}
return $this->render('article/review.html.twig', array(
'form' => $reviewForm->createView(),
));
}
First off, is it advisable to build this form here? Seems like I'm working an awful lot with the Review type here in the Articles controllers. Not sure where else I would build it though.
I'm getting this error:
Neither the property "review" nor one of the methods "getReview()", "review()", "isReview()", "hasReview()", "__get()" exist and have public access in class "AppBundle\Entity\Article".
So I'm really stuck and would appreciate anything that anybody can provide.
EDIT
Here's my Article Entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Article
*
* #ORM\Table(name="article")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
* #UniqueEntity(fields={"name"}, message="Note: That article already existed.")
*/
class Article
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* #var string
*
* #ORM\Column(name="thumbnail", type="string", length=255, nullable=true)
*/
private $thumbnail;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime")
*/
private $createdDate;
/**
* #ORM\ManyToOne(targetEntity="Author", inversedBy="articles", cascade={"persist"})
* #ORM\JoinColumn(name="author_id", referencedColumnName="id")
* #Assert\Valid()
*/
private $author;
/**
* #ORM\OneToMany(targetEntity="Review", mappedBy="article")
*/
private $reviews;
public function __construct()
{
$this->reviews = new ArrayCollection();
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Article
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
*
* #return Article
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set thumbnail
*
* #param string $thumbnail
*
* #return Article
*/
public function setThumbnail($thumbnail)
{
$this->thumbnail = $thumbnail;
return $this;
}
/**
* Get thumbnail
*
* #return string
*/
public function getThumbnail()
{
return $this->thumbnail;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
*
* #return Article
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set authorId
*
* #param integer $authorId
*
* #return Article
*/
public function setAuthorId($authorId)
{
$this->authorId = $authorId;
return $this;
}
/**
* Get authorId
*
* #return int
*/
public function getAuthorId()
{
return $this->authorId;
}
/**
* Set author
*
* #param \AppBundle\Entity\Author $author
*
* #return Article
*/
public function setAuthor(\AppBundle\Entity\Author $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return \AppBundle\Entity\Author
*/
public function getAuthor()
{
return $this->author;
}
/**
* Add review
*
* #param \AppBundle\Entity\Review $review
*
* #return Article
*/
public function addReview(\AppBundle\Entity\Review $review)
{
$this->reviews[] = $review;
return $this;
}
/**
* Remove review
*
* #param \AppBundle\Entity\Review $review
*/
public function removeReview(\AppBundle\Entity\Review $review)
{
$this->reviews->removeElement($review);
}
/**
* Get reviews
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getReviews()
{
return $this->reviews;
}
public function __toString() {
return $this->name;
}
}
Here's my Review entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Review
*
* #ORM\Table(name="review")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ReviewRepository")
*/
class Review
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="rating", type="smallint")
*/
private $rating;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #ORM\ManyToOne(targetEntity="Author", inversedBy="reviews")
* #ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
/**
* #ORM\ManyToOne(targetEntity="Article", inversedBy="reviews")
* #ORM\JoinColumn(name="article_id", referencedColumnName="id")
*/
private $article;
/**
* #var string
*
* #ORM\Column(name="review", type="text", nullable=true)
*/
private $review;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set rating
*
* #param integer $rating
*
* #return Review
*/
public function setRating($rating)
{
$this->rating = $rating;
return $this;
}
/**
* Get rating
*
* #return int
*/
public function getRating()
{
return $this->rating;
}
/**
* Set title
*
* #param string $title
*
* #return Review
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set authorId
*
* #param integer $authorId
*
* #return Review
*/
public function setAuthorId($authorId)
{
$this->authorId = $authorId;
return $this;
}
/**
* Get authorId
*
* #return int
*/
public function getAuthorId()
{
return $this->authorId;
}
/**
* Set review
*
* #param string $review
*
* #return Review
*/
public function setReview($review)
{
$this->review = $review;
return $this;
}
/**
* Get review
*
* #return string
*/
public function getReview()
{
return $this->review;
}
/**
* Set author
*
* #param \AppBundle\Entity\Author $author
*
* #return Review
*/
public function setAuthor(\AppBundle\Entity\Author $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return \AppBundle\Entity\Author
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set article
*
* #param \AppBundle\Entity\Author $article
*
* #return Review
*/
public function setArticle(\AppBundle\Entity\Author $article = null)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* #return \AppBundle\Entity\Author
*/
public function getArticle()
{
return $this->article;
}
}
Here's my article/review.html.twig file
{% extends 'base.html.twig' %}
{% block body %}
<h1>Review Article</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" value="Create" />
{{ form_end(form) }}
<ul>
<li>
Back to the list
</li>
</ul>
{% endblock %}
Update: This code worked
$reviewForm = $this->createFormBuilder($review)
->add('review', ReviewType::class, array("label" => FALSE))
->setAction($this->generateUrl('fundraiser_review', array('id' =>$fundraiser->getId())))
->getForm();
Thanks!
Because you are creating an Article form, and if you look in your Article Entity, you have a reviews array collection, so thus you need to change the code like so:
$reviewForm = $this->createFormBuilder($article);
$reviewForm->add('reviews')
->add('title')
->add('author', AuthorType::class, array("label" => FALSE))
->add('rating', ChoiceType::class, array(
'choices' => array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' =>'5'),
'expanded' => true,
'multiple' => false
))
->getForm();
I think that should solve it. Not 100% certain, but please try it and let us know.
I am trying to create a little CRUD system with 2 entities and a OneToMany relationship between these 2 entities.
My entities are Location & Job. I created the entities and created the crud controller via doctrine for both.
Location CRUD works great but Job only Read & Delete works. When I try to create a new Job it throws the following exception:
Catchable Fatal Error: Object of class DataBundle\Entity\Location
could not be converted to string
I have no idea what it means or what is causing it.
If anyone could help me out that would be realy awesome!
Many thanks in advance!
Here is my code.
Entities:
Location
<?php
namespace DataBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Location
*
* #ORM\Table(name="location")
* #ORM\Entity(repositoryClass="DataBundle\Repository\LocationRepository")
*/
class Location
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="street", type="string", length=255)
*/
private $street;
/**
* #var string
*
* #ORM\Column(name="number", type="string", length=25)
*/
private $number;
/**
* #var string
*
* #ORM\Column(name="postalcode", type="string", length=4)
*/
private $postalcode;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=255)
*/
private $city;
/**
* #var Job[] Available jobs for this location.
*
* #ORM\OneToMany(targetEntity="Job", mappedBy="location")
*/
private $jobs;
public function __construct()
{
$this->jobs = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Location
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set street
*
* #param string $street
*
* #return Location
*/
public function setStreet($street)
{
$this->street = $street;
return $this;
}
/**
* Get street
*
* #return string
*/
public function getStreet()
{
return $this->street;
}
/**
* Set number
*
* #param string $number
*
* #return Location
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* #return string
*/
public function getNumber()
{
return $this->number;
}
/**
* Set postalcode
*
* #param string $postalcode
*
* #return Location
*/
public function setPostalcode($postalcode)
{
$this->postalcode = $postalcode;
return $this;
}
/**
* Get postalcode
*
* #return string
*/
public function getPostalcode()
{
return $this->postalcode;
}
/**
* Set city
*
* #param string $city
*
* #return Location
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Add job
*
* #param \DataBundle\Entity\Job $job
*
* #return Location
*/
public function addJob(\DataBundle\Entity\Job $job)
{
$this->jobs[] = $job;
return $this;
}
/**
* Remove job
*
* #param \DataBundle\Entity\Job $job
*/
public function removeJob(\DataBundle\Entity\Job $job)
{
$this->jobs->removeElement($job);
}
/**
* Get jobs
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getJobs()
{
return $this->jobs;
}
}
Job:
<?php
namespace DataBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Job
*
* #ORM\Table(name="job")
* #ORM\Entity(repositoryClass="DataBundle\Repository\JobRepository")
*/
class Job
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* #var Location
*
* #ORM\ManyToOne(targetEntity="Location", inversedBy="jobs")
* #ORM\JoinColumn(name="location_id", referencedColumnName="id")
*/
private $location;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Job
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* #param string $description
*
* #return Job
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set location
*
* #param \DataBundle\Entity\Location $location
*
* #return Job
*/
public function setLocation(\DataBundle\Entity\Location $location = null)
{
$this->location = $location;
return $this;
}
/**
* Get location
*
* #return \DataBundle\Entity\Location
*/
public function getLocation()
{
return $this->location;
}
}
My new action in the JobController:
/**
* Creates a new job entity.
*
*/
public function newAction(Request $request)
{
$job = new Job();
$form = $this->createForm('DataBundle\Form\JobType', $job);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($job);
$em->flush($job);
return $this->redirectToRoute('job_show', array('id' => $job->getId()));
}
return $this->render('job/new.html.twig', array(
'job' => $job,
'form' => $form->createView(),
));
}
DataBundle/Form/JobTypep
<?php
namespace DataBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class JobType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title')->add('description')->add('location') ;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DataBundle\Entity\Job'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'databundle_job';
}
}
You can't use location as text field in your form, location is object.
You can fix this error by using form data transformers that will convert location into text on form output and text in location object on form submit.
http://symfony.com/doc/current/form/data_transformers.html
Or by using form type EnityType for location:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title')->add('description');
$builder
->add('location', Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
'required' => false,
'expanded' => false,
'multiple' => false,
'class' => 'DataBundle\\Entity\\Location',
'empty_data' => '',
'choice_label' => 'name',
'label' => 'Location',
]);
}
http://symfony.com/doc/current/reference/forms/types/entity.html
Hi I'm writing a little Web App with Symfony.
I have a one to many relation ship between two entities.
Entity one:
/**
* #ORM\Entity
* #ORM\Table(name="Helfer")
*/
class Helfer {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $name;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $lastName;
/**
* #ORM\Column(type="string")
*/
protected $tel;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
* #Assert\Email()
*/
protected $email;
/**
* #ORM\Column(type="string")
*/
protected $organisation;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
* #Assert\Range(min=6, minMessage="Du musst mindestens 6 Jahre Alt sein um bei uns helfen zu können.")
*/
protected $age;
/**
* #ORM\OneToMany(targetEntity="Aufgaben", mappedBy="helfer", cascade={"persist","remove"})
*/
protected $aufgaben;
/**
* #ORM\OneToMany(targetEntity="Zeiten", mappedBy="helfer", cascade={"persist","remove"})
*/
protected $zeiten;
/**
* Constructor
*/
public function __construct()
{
$this->aufgaben = new \Doctrine\Common\Collections\ArrayCollection();
$this->zeiten = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Helfer
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set lastName
*
* #param string $lastName
* #return Helfer
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set tel
*
* #param string $tel
* #return Helfer
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return string
*/
public function getTel()
{
return $this->tel;
}
/**
* Set email
*
* #param string $email
* #return Helfer
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set organisation
*
* #param string $organisation
* #return Helfer
*/
public function setOrganisation($organisation)
{
$this->organisation = $organisation;
return $this;
}
/**
* Get organisation
*
* #return string
*/
public function getOrganisation()
{
return $this->organisation;
}
/**
* Set age
*
* #param integer $age
* #return Helfer
*/
public function setAge($age)
{
$this->age = $age;
return $this;
}
/**
* Get age
*
* #return integer
*/
public function getAge()
{
return $this->age;
}
/**
* Add aufgaben
*
* #param \DLRG\HelferBundle\Entity\Aufgaben $aufgaben
* #return Helfer
*/
public function addAufgaben(\DLRG\HelferBundle\Entity\Aufgaben $aufgaben)
{
$this->aufgaben[] = $aufgaben;
return $this;
}
/**
* Remove aufgaben
*
* #param \DLRG\HelferBundle\Entity\Aufgaben $aufgaben
*/
public function removeAufgaben(\DLRG\HelferBundle\Entity\Aufgaben $aufgaben)
{
$this->aufgaben->removeElement($aufgaben);
}
/**
* Get aufgaben
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getAufgaben()
{
return $this->aufgaben;
}
/**
* Add zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
* #return Helfer
*/
public function addZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten[] = $zeiten;
return $this;
}
/**
* Remove zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
*/
public function removeZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten->removeElement($zeiten);
}
/**
* Get zeiten
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getZeiten()
{
return $this->zeiten;
}
}
Entity two:
/**
* #ORM\Entity
* #ORM\Table(name="Aufgaben")
*/
class Aufgaben {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="string")
*/
protected $beschreibung;
/**
* #ORM\ManyToOne(targetEntity="Helfer", inversedBy="aufgaben")
* #ORM\JoinColumn(name="helfer_aufgaben_id", referencedColumnName="id")
*/
protected $helfer;
/**
* #ORM\OneToMany(targetEntity="Zeiten", mappedBy="aufgaben", cascade={"persist","remove"})
*/
protected $zeiten;
/**
* Constructor
*/
public function __construct()
{
$this->zeiten = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Aufgaben
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set beschreibung
*
* #param string $beschreibung
* #return Aufgaben
*/
public function setBeschreibung($beschreibung)
{
$this->beschreibung = $beschreibung;
return $this;
}
/**
* Get beschreibung
*
* #return string
*/
public function getBeschreibung()
{
return $this->beschreibung;
}
/**
* Set helfer
*
* #param \DLRG\HelferBundle\Entity\Helfer $helfer
* #return Aufgaben
*/
public function setHelfer(\DLRG\HelferBundle\Entity\Helfer $helfer = null)
{
$this->helfer = $helfer;
return $this;
}
/**
* Get helfer
*
* #return \DLRG\HelferBundle\Entity\Helfer
*/
public function getHelfer()
{
return $this->helfer;
}
/**
* Add zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
* #return Aufgaben
*/
public function addZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten[] = $zeiten;
return $this;
}
/**
* Remove zeiten
*
* #param \DLRG\HelferBundle\Entity\Zeiten $zeiten
*/
public function removeZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
{
$this->zeiten->removeElement($zeiten);
}
/**
* Get zeiten
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getZeiten()
{
return $this->zeiten;
}
}
My database has entries in Aufgaben table. Now my user should enter their names and attributes in Helfer.php. Than the user should choose some values from the Aufgaben Entity.
I try to add the Values from Aufgaben.php to a collection.
class HelferType extends AbstractType {
private $aufgaben;
public function __construct($aufgaben) {
$this->aufgaben = $aufgaben;
}
/**
*
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'name' )->add ( 'lastName' )->add ( 'tel' )->add ( 'email' )->add ( 'organisation' )->add ( 'age' );
$builder->add ( 'aufgaben', 'collection', array (
'type' => 'choice',
'options' => array (
'choices' => $this->aufgaben
)
) );
}
But this code doesn't work.
Any Ideas ?
Workflow should be:
Enter User information
Choose one or more values from Aufgaben.php
Save all
Thanks in advance
Depending on your exact needs, one possible solution would be to use an entity form type.
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'name' )->add ( 'lastName' )->add ( 'tel' )->add ( 'email' )->add ( 'organisation' )->add ( 'age' );
$builder->add ( 'aufgaben', 'entity', array (
'class' => 'YourBundle:Aufgaben',
'multiple' => true,
'property' => 'name',
'choices' => $this->aufgaben
)
);
}
This would result in a SELECT tag in your HTML.
maybe this and this can help you.
I resolve this kind of cases overriding the __toString() method in my entity file, not the repository file.
I have a table users with different fields in it. And I also have a table players with a FK user_id in it (along other fields).
Now I have a registerForm to create a user, but I would also like to add a field for a player.
This is my form now:
$builder
->add('userFirstname', 'text', [
'label' => 'Given name',
'attr' => [
'placeholder' => 'Enter your given name.',
],
])
->add('userSurname', 'text', [
'label' => 'Family name',
'attr' => ['placeholder' => 'Enter your family name.'],
])
->add('userType', 'choice', array(
'choices' => array('E' => 'ENTHOUSIAST', 'P' => 'PLAYER', 'T' => 'TRAINER'),
'label' => 'User Type',
'attr' => array('placeholder' => 'User Type')
))
->add('player')
->add('userUsername', 'text', [
'label' => 'Username',
'attr' => ['placeholder' => 'Enter a username.'],
])
->add('userEmail', 'email', [
'label' => 'Email address',
'attr' => ['placeholder' => 'Enter your email address.'],
])
->add('userPassword', 'repeated', [
'type' => 'password',
'first_name' => 'password',
'first_options' => [
'label' => 'Password',
'attr' => ['placeholder' => 'Enter a password.'],
],
'second_name' => 'confirm',
'second_options' => [
'label' => 'Password (repeat)',
'attr' => ['placeholder' => 'Repeat the password.'],
],
'invalid_message' => 'The passwords are not identical.',
])
->add('btn_register', 'submit', [
'label' => 'Register',
])
;
In my players table I have 'player_birthyear'. How can I add this to my form? I already tried to add player.birthyear to my Form but didn't work.
UPDATE:
My Users entities class:
/**
* Users
*
* #ORM\Table(name="users", indexes={#ORM\Index(name="fk_users_roles1_idx", columns={"role_id"})})
* #ORM\Entity
*/
class Users
{
/**
* #var string
*
* #ORM\Column(name="user_username", type="string", length=45, nullable=false)
*/
private $userUsername;
/**
* #var string
*
* #ORM\Column(name="user_firstname", type="string", length=45, nullable=false)
*/
private $userFirstname;
/**
* #var string
*
* #ORM\Column(name="user_surname", type="string", length=255, nullable=false)
*/
private $userSurname;
/**
* #var string
*
* #ORM\Column(name="user_email", type="string", length=255, nullable=false)
*/
private $userEmail;
/**
* #var string
*
* #ORM\Column(name="user_type", type="string", nullable=false)
*/
private $userType;
/**
* #var string
*
* #ORM\Column(name="user_password", type="string", length=60, nullable=false)
*/
private $userPassword;
/**
* #var string
*
* #ORM\Column(name="user_salt", type="string", length=30, nullable=false)
*/
private $userSalt;
/**
* #var string
*
* #ORM\Column(name="user_token", type="string", length=45, nullable=true)
*/
private $userToken;
/**
* #var \DateTime
*
* #ORM\Column(name="user_created", type="datetime", nullable=false)
*/
private $userCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="user_modified", type="datetime", nullable=true)
*/
private $userModified;
/**
* #var \DateTime
*
* #ORM\Column(name="user_deleted", type="datetime", nullable=true)
*/
private $userDeleted;
/**
* #var \DateTime
*
* #ORM\Column(name="user_lastlogin", type="datetime", nullable=true)
*/
private $userLastlogin;
/**
* #var \DateTime
*
* #ORM\Column(name="user_confirmed", type="datetime", nullable=true)
*/
private $userConfirmed;
/**
* #var \DateTime
*
* #ORM\Column(name="user_locked", type="datetime", nullable=true)
*/
private $userLocked;
/**
* #var integer
*
* #ORM\Column(name="user_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Roles
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Roles")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
* })
*/
private $role;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams", inversedBy="user")
* #ORM\JoinTable(name="user_follows_teams",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
* }
* )
*/
private $team;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Competitions", inversedBy="user")
* #ORM\JoinTable(name="user_follows_competitions",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="competition_id", referencedColumnName="competition_id")
* }
* )
*/
private $competition;
/**
* Constructor
*/
public function __construct()
{
$this->team = new \Doctrine\Common\Collections\ArrayCollection();
$this->competition = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set userUsername
*
* #param string $userUsername
* #return Users
*/
public function setUserUsername($userUsername)
{
$this->userUsername = $userUsername;
return $this;
}
/**
* Get userUsername
*
* #return string
*/
public function getUserUsername()
{
return $this->userUsername;
}
/**
* Set userFirstname
*
* #param string $userFirstname
* #return Users
*/
public function setUserFirstname($userFirstname)
{
$this->userFirstname = $userFirstname;
return $this;
}
/**
* Get userFirstname
*
* #return string
*/
public function getUserFirstname()
{
return $this->userFirstname;
}
/**
* Set userSurname
*
* #param string $userSurname
* #return Users
*/
public function setUserSurname($userSurname)
{
$this->userSurname = $userSurname;
return $this;
}
/**
* Get userSurname
*
* #return string
*/
public function getUserSurname()
{
return $this->userSurname;
}
/**
* Set userEmail
*
* #param string $userEmail
* #return Users
*/
public function setUserEmail($userEmail)
{
$this->userEmail = $userEmail;
return $this;
}
/**
* Get userEmail
*
* #return string
*/
public function getUserEmail()
{
return $this->userEmail;
}
/**
* Set userType
*
* #param string $userType
* #return Users
*/
public function setUserType($userType)
{
$this->userType = $userType;
return $this;
}
/**
* Get userType
*
* #return string
*/
public function getUserType()
{
return $this->userType;
}
/**
* Set userPassword
*
* #param string $userPassword
* #return Users
*/
public function setUserPassword($userPassword)
{
$this->userPassword = $userPassword;
return $this;
}
/**
* Get userPassword
*
* #return string
*/
public function getUserPassword()
{
return $this->userPassword;
}
/**
* Set userSalt
*
* #param string $userSalt
* #return Users
*/
public function setUserSalt($userSalt)
{
$this->userSalt = $userSalt;
return $this;
}
/**
* Get userSalt
*
* #return string
*/
public function getUserSalt()
{
return $this->userSalt;
}
/**
* Set userToken
*
* #param string $userToken
* #return Users
*/
public function setUserToken($userToken)
{
$this->userToken = $userToken;
return $this;
}
/**
* Get userToken
*
* #return string
*/
public function getUserToken()
{
return $this->userToken;
}
/**
* Set userCreated
*
* #param \DateTime $userCreated
* #return Users
*/
public function setUserCreated($userCreated)
{
$this->userCreated = $userCreated;
return $this;
}
/**
* Get userCreated
*
* #return \DateTime
*/
public function getUserCreated()
{
return $this->userCreated;
}
/**
* Set userModified
*
* #param \DateTime $userModified
* #return Users
*/
public function setUserModified($userModified)
{
$this->userModified = $userModified;
return $this;
}
/**
* Get userModified
*
* #return \DateTime
*/
public function getUserModified()
{
return $this->userModified;
}
/**
* Set userDeleted
*
* #param \DateTime $userDeleted
* #return Users
*/
public function setUserDeleted($userDeleted)
{
$this->userDeleted = $userDeleted;
return $this;
}
/**
* Get userDeleted
*
* #return \DateTime
*/
public function getUserDeleted()
{
return $this->userDeleted;
}
/**
* Set userLastlogin
*
* #param \DateTime $userLastlogin
* #return Users
*/
public function setUserLastlogin($userLastlogin)
{
$this->userLastlogin = $userLastlogin;
return $this;
}
/**
* Get userLastlogin
*
* #return \DateTime
*/
public function getUserLastlogin()
{
return $this->userLastlogin;
}
/**
* Set userConfirmed
*
* #param \DateTime $userConfirmed
* #return Users
*/
public function setUserConfirmed($userConfirmed)
{
$this->userConfirmed = $userConfirmed;
return $this;
}
/**
* Get userConfirmed
*
* #return \DateTime
*/
public function getUserConfirmed()
{
return $this->userConfirmed;
}
/**
* Set userLocked
*
* #param \DateTime $userLocked
* #return Users
*/
public function setUserLocked($userLocked)
{
$this->userLocked = $userLocked;
return $this;
}
/**
* Get userLocked
*
* #return \DateTime
*/
public function getUserLocked()
{
return $this->userLocked;
}
/**
* Get userId
*
* #return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set role
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Roles $role
* #return Users
*/
public function setRole(\VolleyScout\VolleyScoutBundle\Entity\Roles $role = null)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Roles
*/
public function getRole()
{
return $this->role;
}
/**
* Add team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
* #return Users
*/
public function addTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
{
$this->team[] = $team;
return $this;
}
/**
* Remove team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
*/
public function removeTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
{
$this->team->removeElement($team);
}
/**
* Get team
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTeam()
{
return $this->team;
}
/**
* Add competition
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
* #return Users
*/
public function addCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
{
$this->competition[] = $competition;
return $this;
}
/**
* Remove competition
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
*/
public function removeCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
{
$this->competition->removeElement($competition);
}
/**
* Get competition
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCompetition()
{
return $this->competition;
}
}
My Players entities class:
/**
* Players
*
* #ORM\Table(name="players", indexes={#ORM\Index(name="fk_players_users1_idx", columns={"user_id"}), #ORM\Index(name="fk_players_teams1_idx", columns={"team_id"}), #ORM\Index(name="fk_players_myteam1_idx", columns={"myteam_id"})})
* #ORM\Entity
*/
class Players
{
/**
* #var string
*
* #ORM\Column(name="player_name", type="string", length=255, nullable=false)
*/
private $playerName;
/**
* #var string
*
* #ORM\Column(name="player_licensenumber", type="string", length=45, nullable=false)
*/
private $playerLicensenumber;
/**
* #var string
*
* #ORM\Column(name="player_position", type="string", nullable=false)
*/
private $playerPosition;
/**
* #var \DateTime
*
* #ORM\Column(name="player_birthyear", type="datetime", nullable=true)
*/
private $playerBirthyear;
/**
* #var integer
*
* #ORM\Column(name="player_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $playerId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Myteam
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Teams
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
* })
*/
private $team;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set playerName
*
* #param string $playerName
* #return Players
*/
public function setPlayerName($playerName)
{
$this->playerName = $playerName;
return $this;
}
/**
* Get playerName
*
* #return string
*/
public function getPlayerName()
{
return $this->playerName;
}
/**
* Set playerLicensenumber
*
* #param string $playerLicensenumber
* #return Players
*/
public function setPlayerLicensenumber($playerLicensenumber)
{
$this->playerLicensenumber = $playerLicensenumber;
return $this;
}
/**
* Get playerLicensenumber
*
* #return string
*/
public function getPlayerLicensenumber()
{
return $this->playerLicensenumber;
}
/**
* Set playerPosition
*
* #param string $playerPosition
* #return Players
*/
public function setPlayerPosition($playerPosition)
{
$this->playerPosition = $playerPosition;
return $this;
}
/**
* Get playerPosition
*
* #return string
*/
public function getPlayerPosition()
{
return $this->playerPosition;
}
/**
* Set playerBirthyear
*
* #param \DateTime $playerBirthyear
* #return Players
*/
public function setPlayerBirthyear($playerBirthyear)
{
$this->playerBirthyear = $playerBirthyear;
return $this;
}
/**
* Get playerBirthyear
*
* #return \DateTime
*/
public function getPlayerBirthyear()
{
return $this->playerBirthyear;
}
/**
* Get playerId
*
* #return integer
*/
public function getPlayerId()
{
return $this->playerId;
}
/**
* Set myteam
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam
* #return Players
*/
public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Myteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
* #return Players
*/
public function setTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team = null)
{
$this->team = $team;
return $this;
}
/**
* Get team
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Teams
*/
public function getTeam()
{
return $this->team;
}
/**
* Set user
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Users $user
* #return Players
*/
public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
When I try add a player field I get this error:
Neither the property "player" nor one of the methods "getPlayer()", "isPlayer()", "hasPlayer()", "__get()" exist and have public access in class "VolleyScout\VolleyScoutBundle\Entity\Users".
UPDATE 2:
And I also have a FK in my players table 'team_id'. So when I create a user I would like to also select a team. So what I want is a list from all the teams in the database and set is as a choice field type in my Form. Do you know how I can place that in my formtype?
UPDATE 3:
I've added the following to my Users entity class:
private $player;
/**
* Get player
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Players
*/
public function getPlayer() {
return $this->player;
}
/**
* Set player
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Players $player
* #return Users
*/
public function setPlayer(\VolleyScout\VolleyScoutBundle\Entity\Players $player){
$this->player = $player;
}
In my formtype I've added:
->add('player')
Now in my form I want to do the following:
{{ form_label(registerForm.player.playerLicensenumber, label|default(),
Because in my Players entity class I have playerLicensenumber but I always get this error:
Method "playerLicensenumber" for object "Symfony\Component\Form\FormView" does not exist in VolleyScoutBundle:User:register.html.twig at line 57
UPDATE 4:
Is this a good solution:
Added this to my register form:
->add('player', new PlayerType())
And in my playertype I have the fields from player table.
You have to add a setter and a getter for your "player" field in your User entity.
public function getPlayer() {
return $this->player;
}
public function setPlayer(\Path\to\player\entity $player){
$this->player = $player;
}
For the team list in your form, you have to add an "entity" field type. Here are the docs for more info : http://symfony.com/doc/current/reference/forms/types/entity.html