I firstly created the database and then generated the entities from it. After that I generated the getters and setters in the entities using:
php app/console doctrine:generate:entities DigitalManager
Later on I decided to create the association (One to Many) between the entities, so I added the respective commands in my Entities and what I got is this:
Party Entity:
<?php
namespace DigitalManager\Bundle\ERPBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Party
*
* #ORM\Table(name="party")
* #ORM\Entity
*/
class Party
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
/**
* #ORM\ManyToOne(targetEntity="Account", inversedBy="parties")
* #ORM\JoinColumn(name="account_id", referencedColumnName="aid")
*/
public $account;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=45, nullable=true)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="phone_fax", type="string", length=45, nullable=true)
*/
private $phoneFax;
/**
* #var string
*
* #ORM\Column(name="fax", type="string", length=45, nullable=true)
*/
private $fax;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=45, nullable=true)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="mobile", type="string", length=45, nullable=true)
*/
private $mobile;
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=45, nullable=true)
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=45, nullable=true)
*/
private $city;
/**
* #var string
*
* #ORM\Column(name="phone_res", type="string", length=45, nullable=true)
*/
private $phoneRes;
/**
* #var string
*
* #ORM\Column(name="remarks", type="string", length=45, nullable=true)
*/
private $remarks;
/**
* #var boolean
*
* #ORM\Column(name="active", type="boolean", nullable=true)
*/
private $active;
/**
* #var integer
*
* #ORM\Column(name="limit", type="integer", nullable=true)
*/
private $limit;
/**
* #var integer
*
* #ORM\Column(name="term", type="integer", nullable=true)
*/
private $term;
/**
* #var string
*
* #ORM\Column(name="officer_id", type="string", length=45, nullable=true)
*/
private $officerId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="cnic", type="string", length=45, nullable=true)
*/
private $cnic;
/**
* #var string
*
* #ORM\Column(name="ntn", type="string", length=45, nullable=true)
*/
private $ntn;
/**
* #var boolean
*
* #ORM\Column(name="L_O", type="boolean", nullable=true)
*/
private $lO;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=45, nullable=true)
*/
private $type;
/**
* #var integer
*
* #ORM\Column(name="acc_id", type="integer", nullable=true)
*/
private $accId;
/**
* #var integer
*
* #ORM\Column(name="party_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $partyId;
/**
* Set name
*
* #param string $name
* #return Party
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
* #return Party
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set phoneFax
*
* #param string $phoneFax
* #return Party
*/
public function setPhoneFax($phoneFax)
{
$this->phoneFax = $phoneFax;
return $this;
}
/**
* Get phoneFax
*
* #return string
*/
public function getPhoneFax()
{
return $this->phoneFax;
}
/**
* Set fax
*
* #param string $fax
* #return Party
*/
public function setFax($fax)
{
$this->fax = $fax;
return $this;
}
/**
* Get fax
*
* #return string
*/
public function getFax()
{
return $this->fax;
}
/**
* Set email
*
* #param string $email
* #return Party
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set mobile
*
* #param string $mobile
* #return Party
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile
*
* #return string
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Set country
*
* #param string $country
* #return Party
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set city
*
* #param string $city
* #return Party
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set phoneRes
*
* #param string $phoneRes
* #return Party
*/
public function setPhoneRes($phoneRes)
{
$this->phoneRes = $phoneRes;
return $this;
}
/**
* Get phoneRes
*
* #return string
*/
public function getPhoneRes()
{
return $this->phoneRes;
}
/**
* Set remarks
*
* #param string $remarks
* #return Party
*/
public function setRemarks($remarks)
{
$this->remarks = $remarks;
return $this;
}
/**
* Get remarks
*
* #return string
*/
public function getRemarks()
{
return $this->remarks;
}
/**
* Set active
*
* #param boolean $active
* #return Party
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* #return boolean
*/
public function getActive()
{
return $this->active;
}
/**
* Set limit
*
* #param integer $limit
* #return Party
*/
public function setLimit($limit)
{
$this->limit = $limit;
return $this;
}
/**
* Get limit
*
* #return integer
*/
public function getLimit()
{
return $this->limit;
}
/**
* Set term
*
* #param integer $term
* #return Party
*/
public function setTerm($term)
{
$this->term = $term;
return $this;
}
/**
* Get term
*
* #return integer
*/
public function getTerm()
{
return $this->term;
}
/**
* Set officerId
*
* #param string $officerId
* #return Party
*/
public function setOfficerId($officerId)
{
$this->officerId = $officerId;
return $this;
}
/**
* Get officerId
*
* #return string
*/
public function getOfficerId()
{
return $this->officerId;
}
/**
* Set date
*
* #param \DateTime $date
* #return Party
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set cnic
*
* #param string $cnic
* #return Party
*/
public function setCnic($cnic)
{
$this->cnic = $cnic;
return $this;
}
/**
* Get cnic
*
* #return string
*/
public function getCnic()
{
return $this->cnic;
}
/**
* Set ntn
*
* #param string $ntn
* #return Party
*/
public function setNtn($ntn)
{
$this->ntn = $ntn;
return $this;
}
/**
* Get ntn
*
* #return string
*/
public function getNtn()
{
return $this->ntn;
}
/**
* Set lO
*
* #param boolean $lO
* #return Party
*/
public function setLO($lO)
{
$this->lO = $lO;
return $this;
}
/**
* Get lO
*
* #return boolean
*/
public function getLO()
{
return $this->lO;
}
/**
* Set type
*
* #param string $type
* #return Party
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return string
*/
public function getType()
{
return $this->type;
}
/**
* Set accId
*
* #param integer $accId
* #return Party
*/
public function setAccId($accId)
{
$this->accId = $accId;
return $this;
}
/**
* Get accId
*
* #return integer
*/
public function getAccId()
{
return $this->accId;
}
/**
* Get partyId
*
* #return integer
*/
public function getPartyId()
{
return $this->partyId;
}
}
Account Entity
<?php
namespace DigitalManager\Bundle\ERPBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Account
*
* #ORM\Table(name="account")
* #ORM\Entity
*/
class Account
{
public function __construct()
{
parent::__construct();
// The line below must be here as a single category is to be mapped to many products, so an ArrayCollection is required instead of Array.
$this->parties = new ArrayCollection();
}
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
// The metadata above the $products property of the Category object is less important, and simply tells
// Doctrine to look at the Product.category property to figure out how the relationship is mapped.
/**
* #ORM\OneToMany(targetEntity="Party", mappedBy="account")
*/
protected $parties;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=200, nullable=true)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="level1", type="string", length=45, nullable=true)
*/
private $level1;
/**
* #var string
*
* #ORM\Column(name="level2", type="string", length=45, nullable=true)
*/
private $level2;
/**
* #var string
*
* #ORM\Column(name="level3", type="string", length=45, nullable=true)
*/
private $level3;
/**
* #var string
*
* #ORM\Column(name="contact", type="string", length=45, nullable=true)
*/
private $contact;
/**
* #var integer
*
* #ORM\Column(name="aid", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $aid;
/**
* Set name
*
* #param string $name
* #return Account
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
* #return Account
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set level1
*
* #param string $level1
* #return Account
*/
public function setLevel1($level1)
{
$this->level1 = $level1;
return $this;
}
/**
* Get level1
*
* #return string
*/
public function getLevel1()
{
return $this->level1;
}
/**
* Set level2
*
* #param string $level2
* #return Account
*/
public function setLevel2($level2)
{
$this->level2 = $level2;
return $this;
}
/**
* Get level2
*
* #return string
*/
public function getLevel2()
{
return $this->level2;
}
/**
* Set level3
*
* #param string $level3
* #return Account
*/
public function setLevel3($level3)
{
$this->level3 = $level3;
return $this;
}
/**
* Get level3
*
* #return string
*/
public function getLevel3()
{
return $this->level3;
}
/**
* Set contact
*
* #param string $contact
* #return Account
*/
public function setContact($contact)
{
$this->contact = $contact;
return $this;
}
/**
* Get contact
*
* #return string
*/
public function getContact()
{
return $this->contact;
}
/**
* Get aid
*
* #return integer
*/
public function getAid()
{
return $this->aid;
}
}
After adding these association commands, I tried to generate the getters and setters for these using:
php app/console doctrine:generate:entities DigitalManager
But it doesn't seem to generate the getters and setters for these associated attributes. I have looked up the syntax through the Symfony2 documentation and it was the same for creating associations. Can anyone please tell me what's wrong here, why does it not generate the getters and setters, although the attributes are there?
It may be trying to read the information from somewhere else. Is there an XML file for the configuration of this entity under Bundle/Resources/config/doctrine/?
There are two process to generate an entity and create a database and so on process.
I think you can do this way
1. create a entity using command
php app/console doctrine:generate:entity
you provide shortcut name Like AcmeHelloBundle. After that it will ask you all the filed name of a table. step by step you create the table.
2. If you not create your database then set database configuration file and run this command
php app/console doctrine:database:create
3. After create the database you need to create the table depending on your entity that create in list 2 and run the folloing command like
php app/console doctrine:schema:update --force
4.Now you can create association with other entity. I just show here create entity but you need to create all entity that need for your project. After perfect association you need to run this command
php app/console doctrine:generate:entities DigitalManager
5. Finnaly you need to perform this command again
php app/console doctrine:schema:update --force
This may work fine.
Related
I have 15 Entity Classes, and I want to put getEntityName static function in there but I don't want to make code duplication, and some of my entities extends vendor related class so I cannot put an abstract class to extend from these entities. I wanted to use traits.
<?php
namespace FrontendBundle\Traits\Entity;
trait GetEntityNameTrait {
public static function getEntityName()
{
return str_replace('\\', '\\\\', get_parent_class());
}
}
I use this trait in any of this entities, but I'm getting following compile error.
Compile Error: Cannot redeclare class FrontendBundle\Entity\Country
I tried several reflection methods all are the same result is this a bug or something is not logical ?
Others:
get_parent_class()
get_class()
get_called_class()
__CLASS__
<?php
namespace FrontendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FrontendBundle\EntityInterface;
use Symfony\Component\Validator\Constraints as Assert;
use FrontendBundle\Traits\Entity\GetEntityNameTrait;
/**
* Country
*
* #ORM\Table(name="country", uniqueConstraints={#ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), #ORM\UniqueConstraint(name="iso_UNIQUE", columns={"iso"}), #ORM\UniqueConstraint(name="slug_tr_UNIQUE", columns={"slug_tr"}), #ORM\UniqueConstraint(name="slug_en_UNIQUE", columns={"slug_en"})})
* #ORM\Entity(repositoryClass="FrontendBundle\Repository\CountryRepository")
* #ORM\HasLifecycleCallbacks
*/
class Country implements EntityInterface
{
use GetEntityNameTrait;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="iso", type="string", length=2, nullable=false)
*/
private $iso;
/**
* #var string
*
* #ORM\Column(name="iso3", type="string", length=3, nullable=true)
*/
private $iso3;
/**
* #var string
*
* #ORM\Column(name="currency_code", type="string", length=3, nullable=true)
*/
private $currencyCode;
/**
* #var string
*
* #ORM\Column(name="currency_name", type="string", length=20, nullable=true)
*/
private $currencyName;
/**
* #var string
*
* #ORM\Column(name="currency_symbol", type="string", length=5, nullable=true)
*/
private $currencySymbol;
/**
* #var string
*
* #ORM\Column(name="name_en", type="string", length=80, nullable=false)
*/
private $nameEn;
/**
* #var string
*
* #ORM\Column(name="name_tr", type="string", length=80, nullable=false)
*/
private $nameTr;
/**
* #var integer
*
* #ORM\Column(name="numcode", type="smallint", nullable=true)
*/
private $numcode;
/**
* #var integer
*
* #ORM\Column(name="phonecode", type="integer", nullable=false)
*/
private $phonecode;
/**
* #var float
*
* #ORM\Column(name="latitude", type="float", precision=18, scale=10, nullable=false)
*/
private $latitude;
/**
* #var float
*
* #ORM\Column(name="longitude", type="float", precision=18, scale=10, nullable=false)
*/
private $longitude;
/**
* #var string
*
* #ORM\Column(name="slug_en", type="string", length=50, nullable=false)
*/
private $slugEn;
/**
* #var string
*
* #ORM\Column(name="slug_tr", type="string", length=50, nullable=false)
*/
private $slugTr;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set iso
*
* #param string $iso
* #return Country
*/
public function setIso($iso)
{
$this->iso = $iso;
return $this;
}
/**
* Get iso
*
* #return string
*/
public function getIso()
{
return $this->iso;
}
/**
* Set iso3
*
* #param string $iso3
* #return Country
*/
public function setIso3($iso3)
{
$this->iso3 = $iso3;
return $this;
}
/**
* Get iso3
*
* #return string
*/
public function getIso3()
{
return $this->iso3;
}
/**
* Set currencyCode
*
* #param string $currencyCode
* #return Country
*/
public function setCurrencyCode($currencyCode)
{
$this->currencyCode = $currencyCode;
return $this;
}
/**
* Get currencyCode
*
* #return string
*/
public function getCurrencyCode()
{
return $this->currencyCode;
}
/**
* Set currencyName
*
* #param string $currencyName
* #return Country
*/
public function setCurrencyName($currencyName)
{
$this->currencyName = $currencyName;
return $this;
}
/**
* Get currencyName
*
* #return string
*/
public function getCurrencyName()
{
return $this->currencyName;
}
/**
* Set currencySymbol
*
* #param string $currencySymbol
* #return Country
*/
public function setCurrencySymbol($currencySymbol)
{
$this->currencySymbol = $currencySymbol;
return $this;
}
/**
* Get currencySymbol
*
* #return string
*/
public function getCurrencySymbol()
{
return $this->currencySymbol;
}
/**
* Set nameEn
*
* #param string $nameEn
* #return Country
*/
public function setNameEn($nameEn)
{
$this->nameEn = $nameEn;
return $this;
}
/**
* Get nameEn
*
* #return string
*/
public function getNameEn()
{
return $this->nameEn;
}
/**
* Set nameTr
*
* #param string $nameTr
* #return Country
*/
public function setNameTr($nameTr)
{
$this->nameTr = $nameTr;
return $this;
}
/**
* Get nameTr
*
* #return string
*/
public function getNameTr()
{
return $this->nameTr;
}
/**
* Set numcode
*
* #param integer $numcode
* #return Country
*/
public function setNumcode($numcode)
{
$this->numcode = $numcode;
return $this;
}
/**
* Get numcode
*
* #return integer
*/
public function getNumcode()
{
return $this->numcode;
}
/**
* Set phonecode
*
* #param integer $phonecode
* #return Country
*/
public function setPhonecode($phonecode)
{
$this->phonecode = $phonecode;
return $this;
}
/**
* Get phonecode
*
* #return integer
*/
public function getPhonecode()
{
return $this->phonecode;
}
/**
* Set latitude
*
* #param float $latitude
* #return Country
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return float
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param float $longitude
* #return Country
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return float
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Set slugEn
*
* #param string $slugEn
* #return Country
*/
public function setSlugEn($slugEn)
{
$this->slugEn = $slugEn;
return $this;
}
/**
* Get slugEn
*
* #return string
*/
public function getSlugEn()
{
return $this->slugEn;
}
/**
* Set slugTr
*
* #param string $slugTr
* #return Country
*/
public function setSlugTr($slugTr)
{
$this->slugTr = $slugTr;
return $this;
}
/**
* Get slugTr
*
* #return string
*/
public function getSlugTr()
{
return $this->slugTr;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Country
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Country
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
*
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('now'));
}
}
}
I have project which have different bundles. I want to create or divide entities in different bundle how can I create this. I am new in symfony please help me . I created but I can not do mapping correctly . a table which have primary key can't use in another bundle.
Here is my Entity
namespace DomainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* WebsiteDomainConfigTest
*
* #ORM\Table(name="website_domain_config_test", indexes={#ORM\Index(name="fk_website_domain_config_test_1_idx", columns={"vendor_id"}), #ORM\Index(name="fk_website_domain_config_test_2_idx", columns={"country_id"})})
* #ORM\Entity
*/
class WebsiteDomainConfigTest
{
/**
* #var string
*
* #ORM\Column(name="domain", type="string", length=255, nullable=true)
*/
private $domain;
/**
* #var string
*
* #ORM\Column(name="vendor_code", type="string", length=15, nullable=true)
*/
private $vendorCode;
/**
* #var string
*
* #ORM\Column(name="domain_path", type="string", length=255, nullable=true)
*/
private $domainPath;
/**
* #var string
*
* #ORM\Column(name="assets_path", type="string", length=45, nullable=true)
*/
private $assetsPath;
/**
* #var string
*
* #ORM\Column(name="language", type="string", length=3, nullable=true)
*/
private $language;
/**
* #var string
*
* #ORM\Column(name="affiliate", type="string", length=25, nullable=true)
*/
private $affiliate;
/**
* #var string
*
* #ORM\Column(name="affiliate_logo", type="string", length=255, nullable=true)
*/
private $affiliateLogo;
/**
* #var string
*
* #ORM\Column(name="affiliate_address", type="string", length=255, nullable=true)
*/
private $affiliateAddress;
/**
* #var integer
*
* #ORM\Column(name="status", type="integer", nullable=true)
*/
private $status;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \MobileSplash\SplashRequestBundle\Entity\Countries
*
* #ORM\ManyToOne(targetEntity="MobileSplash\SplashRequestBundle\Entity\Countries")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="country_id", referencedColumnName="id")
* })
*/
private $country;
/**
* #var \MobileSplash\SplashRequestBundle\Entity\Vendors
*
* #ORM\ManyToOne(targetEntity="MobileSplash\SplashRequestBundle\Entity\Vendors")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vendor_id", referencedColumnName="id")
* })
*/
private $vendor;
/**
* Set domain
*
* #param string $domain
* #return WebsiteDomainConfigTest
*/
public function setDomain($domain)
{
$this->domain = $domain;
return $this;
}
/**
* Get domain
*
* #return string
*/
public function getDomain()
{
return $this->domain;
}
/**
* Set vendorCode
*
* #param string $vendorCode
* #return WebsiteDomainConfigTest
*/
public function setVendorCode($vendorCode)
{
$this->vendorCode = $vendorCode;
return $this;
}
/**
* Get vendorCode
*
* #return string
*/
public function getVendorCode()
{
return $this->vendorCode;
}
/**
* Set domainPath
*
* #param string $domainPath
* #return WebsiteDomainConfigTest
*/
public function setDomainPath($domainPath)
{
$this->domainPath = $domainPath;
return $this;
}
/**
* Get domainPath
*
* #return string
*/
public function getDomainPath()
{
return $this->domainPath;
}
/**
* Set assetsPath
*
* #param string $assetsPath
* #return WebsiteDomainConfigTest
*/
public function setAssetsPath($assetsPath)
{
$this->assetsPath = $assetsPath;
return $this;
}
/**
* Get assetsPath
*
* #return string
*/
public function getAssetsPath()
{
return $this->assetsPath;
}
/**
* Set language
*
* #param string $language
* #return WebsiteDomainConfigTest
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* #return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Set affiliate
*
* #param string $affiliate
* #return WebsiteDomainConfigTest
*/
public function setAffiliate($affiliate)
{
$this->affiliate = $affiliate;
return $this;
}
/**
* Get affiliate
*
* #return string
*/
public function getAffiliate()
{
return $this->affiliate;
}
/**
* Set affiliateLogo
*
* #param string $affiliateLogo
* #return WebsiteDomainConfigTest
*/
public function setAffiliateLogo($affiliateLogo)
{
$this->affiliateLogo = $affiliateLogo;
return $this;
}
/**
* Get affiliateLogo
*
* #return string
*/
public function getAffiliateLogo()
{
return $this->affiliateLogo;
}
/**
* Set affiliateAddress
*
* #param string $affiliateAddress
* #return WebsiteDomainConfigTest
*/
public function setAffiliateAddress($affiliateAddress)
{
$this->affiliateAddress = $affiliateAddress;
return $this;
}
/**
* Get affiliateAddress
*
* #return string
*/
public function getAffiliateAddress()
{
return $this->affiliateAddress;
}
/**
* Set status
*
* #param integer $status
* #return WebsiteDomainConfigTest
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set country
*
* #param \MobileSplash\SplashRequestBundle\Entity\Countries $country
* #return WebsiteDomainConfigTest
*/
public function setCountry(\MobileSplash\SplashRequestBundle\Entity\Countries $country = null)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return \MobileSplash\SplashRequestBundle\Entity\Countries
*/
public function getCountry()
{
return $this->country;
}
/**
* Set vendor
*
* #param \MobileSplash\SplashRequestBundle\Entity\Vendors $vendor
* #return WebsiteDomainConfigTest
*/
public function setVendor(\MobileSplash\SplashRequestBundle\Entity\Vendors $vendor = null)
{
$this->vendor = $vendor;
return $this;
}
/**
* Get vendor
*
* #return \MobileSplash\SplashRequestBundle\Entity\Vendors
*/
public function getVendor()
{
return $this->vendor;
}
}
in your annotation use namespace like this:
targetEntity="\Mj\FooBundle\Entity\Foo"
I'm very new to Symfony 2.0 and doctrine. I have generete my entity refer to the cookbook 'How to Generate Entities from an Existing Database'
my relation ship :
client have many payement
payement to one client
Entity : Client.php
<?php
namespace Auto\EcoleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Client
*
* #ORM\Table(name="client", indexes={#ORM\Index(name="fk_client_delegation1_idx", columns={"delegation_id"})})
* #ORM\Entity
*/
class Client
{
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=45, nullable=false)
*/
private $nom;
/**
* #var \DateTime
*
* #ORM\Column(name="date_nais", type="date", nullable=true)
*/
private $dateNais;
/**
* #var string
*
* #ORM\Column(name="profession", type="string", length=45, nullable=true)
*/
private $profession;
/**
* #var string
*
* #ORM\Column(name="passport_num", type="string", length=45, nullable=true)
*/
private $passportNum;
/**
* #var string
*
* #ORM\Column(name="cin_num", type="string", length=45, nullable=true)
*/
private $cinNum;
/**
* #var \DateTime
*
* #ORM\Column(name="cin_date", type="date", nullable=true)
*/
private $cinDate;
/**
* #var string
*
* #ORM\Column(name="adresse", type="string", length=100, nullable=true)
*/
private $adresse;
/**
* #var string
*
* #ORM\Column(name="code_postal", type="string", length=45, nullable=true)
*/
private $codePostal;
/**
* #var boolean
*
* #ORM\Column(name="code", type="boolean", nullable=true)
*/
private $code;
/**
* #var string
*
* #ORM\Column(name="tel", type="string", length=45, nullable=true)
*/
private $tel;
/**
* #var string
*
* #ORM\Column(name="fax", type="string", length=45, nullable=true)
*/
private $fax;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=50, nullable=true)
*/
private $email;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Auto\EcoleBundle\Entity\Delegation
*
* #ORM\ManyToOne(targetEntity="Auto\EcoleBundle\Entity\Delegation")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="delegation_id", referencedColumnName="id")
* })
*/
private $delegation;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Auto\EcoleBundle\Entity\ExamenCode", inversedBy="client")
* #ORM\JoinTable(name="client_has_examen_code",
* joinColumns={
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="examen_code_id", referencedColumnName="id")
* }
* )
*/
private $examenCode;
/**
* Constructor
*/
public function __construct()
{
$this->examenCode = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set nom
*
* #param string $nom
* #return Client
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* #return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set dateNais
*
* #param \DateTime $dateNais
* #return Client
*/
public function setDateNais($dateNais)
{
$this->dateNais = $dateNais;
return $this;
}
/**
* Get dateNais
*
* #return \DateTime
*/
public function getDateNais()
{
return $this->dateNais;
}
/**
* Set profession
*
* #param string $profession
* #return Client
*/
public function setProfession($profession)
{
$this->profession = $profession;
return $this;
}
/**
* Get profession
*
* #return string
*/
public function getProfession()
{
return $this->profession;
}
/**
* Set passportNum
*
* #param string $passportNum
* #return Client
*/
public function setPassportNum($passportNum)
{
$this->passportNum = $passportNum;
return $this;
}
/**
* Get passportNum
*
* #return string
*/
public function getPassportNum()
{
return $this->passportNum;
}
/**
* Set cinNum
*
* #param string $cinNum
* #return Client
*/
public function setCinNum($cinNum)
{
$this->cinNum = $cinNum;
return $this;
}
/**
* Get cinNum
*
* #return string
*/
public function getCinNum()
{
return $this->cinNum;
}
/**
* Set cinDate
*
* #param \DateTime $cinDate
* #return Client
*/
public function setCinDate($cinDate)
{
$this->cinDate = $cinDate;
return $this;
}
/**
* Get cinDate
*
* #return \DateTime
*/
public function getCinDate()
{
return $this->cinDate;
}
/**
* Set adresse
*
* #param string $adresse
* #return Client
*/
public function setAdresse($adresse)
{
$this->adresse = $adresse;
return $this;
}
/**
* Get adresse
*
* #return string
*/
public function getAdresse()
{
return $this->adresse;
}
/**
* Set codePostal
*
* #param string $codePostal
* #return Client
*/
public function setCodePostal($codePostal)
{
$this->codePostal = $codePostal;
return $this;
}
/**
* Get codePostal
*
* #return string
*/
public function getCodePostal()
{
return $this->codePostal;
}
/**
* Set code
*
* #param boolean $code
* #return Client
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* #return boolean
*/
public function getCode()
{
return $this->code;
}
/**
* Set tel
*
* #param string $tel
* #return Client
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return string
*/
public function getTel()
{
return $this->tel;
}
/**
* Set fax
*
* #param string $fax
* #return Client
*/
public function setFax($fax)
{
$this->fax = $fax;
return $this;
}
/**
* Get fax
*
* #return string
*/
public function getFax()
{
return $this->fax;
}
/**
* Set email
*
* #param string $email
* #return Client
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set delegation
*
* #param \Auto\EcoleBundle\Entity\Delegation $delegation
* #return Client
*/
public function setDelegation(\Auto\EcoleBundle\Entity\Delegation $delegation = null)
{
$this->delegation = $delegation;
return $this;
}
/**
* Get delegation
*
* #return \Auto\EcoleBundle\Entity\Delegation
*/
public function getDelegation()
{
return $this->delegation;
}
/**
* Add examenCode
*
* #param \Auto\EcoleBundle\Entity\ExamenCode $examenCode
* #return Client
*/
public function addExamenCode(\Auto\EcoleBundle\Entity\ExamenCode $examenCode)
{
$this->examenCode[] = $examenCode;
return $this;
}
/**
* Remove examenCode
*
* #param \Auto\EcoleBundle\Entity\ExamenCode $examenCode
*/
public function removeExamenCode(\Auto\EcoleBundle\Entity\ExamenCode $examenCode)
{
$this->examenCode->removeElement($examenCode);
}
/**
* Get examenCode
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getExamenCode()
{
return $this->examenCode;
}
}
Entity :Payement.php
<?php
namespace Auto\EcoleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Payement
*
* #ORM\Table(name="payement", indexes={#ORM\Index(name="fk_payement_client1_idx", columns={"client_id"}), #ORM\Index(name="fk_payement_payement_type1_idx", columns={"payement_type_id"}), #ORM\Index(name="fk_payement_rebrique1_idx", columns={"rebrique_id"}), #ORM\Index(name="fk_payement_banque1_idx", columns={"banque_id"})})
* #ORM\Entity
*/
class Payement
{
/**
* #var float
*
* #ORM\Column(name="montant", type="float", precision=10, scale=0, nullable=true)
*/
private $montant;
/**
* #var \DateTime
*
* #ORM\Column(name="date_payement", type="date", nullable=true)
*/
private $datePayement;
/**
* #var string
*
* #ORM\Column(name="remarque", type="string", length=45, nullable=true)
*/
private $remarque;
/**
* #var string
*
* #ORM\Column(name="num_cheque", type="string", length=45, nullable=true)
*/
private $numCheque;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Auto\EcoleBundle\Entity\Banque
*
* #ORM\ManyToOne(targetEntity="Auto\EcoleBundle\Entity\Banque")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="banque_id", referencedColumnName="id")
* })
*/
private $banque;
/**
* #var \Auto\EcoleBundle\Entity\PayementRebrique
*
* #ORM\ManyToOne(targetEntity="Auto\EcoleBundle\Entity\PayementRebrique")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="rebrique_id", referencedColumnName="id")
* })
*/
private $rebrique;
/**
* #var \Auto\EcoleBundle\Entity\PayementType
*
* #ORM\ManyToOne(targetEntity="Auto\EcoleBundle\Entity\PayementType")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="payement_type_id", referencedColumnName="id")
* })
*/
private $payementType;
/**
* #var \Auto\EcoleBundle\Entity\Client
*
* #ORM\ManyToOne(targetEntity="Auto\EcoleBundle\Entity\Client")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
* })
*/
private $client;
/**
* Set montant
*
* #param float $montant
* #return Payement
*/
public function setMontant($montant)
{
$this->montant = $montant;
return $this;
}
/**
* Get montant
*
* #return float
*/
public function getMontant()
{
return $this->montant;
}
/**
* Set datePayement
*
* #param \DateTime $datePayement
* #return Payement
*/
public function setDatePayement($datePayement)
{
$this->datePayement = $datePayement;
return $this;
}
/**
* Get datePayement
*
* #return \DateTime
*/
public function getDatePayement()
{
return $this->datePayement;
}
/**
* Set remarque
*
* #param string $remarque
* #return Payement
*/
public function setRemarque($remarque)
{
$this->remarque = $remarque;
return $this;
}
/**
* Get remarque
*
* #return string
*/
public function getRemarque()
{
return $this->remarque;
}
/**
* Set numCheque
*
* #param string $numCheque
* #return Payement
*/
public function setNumCheque($numCheque)
{
$this->numCheque = $numCheque;
return $this;
}
/**
* Get numCheque
*
* #return string
*/
public function getNumCheque()
{
return $this->numCheque;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set banque
*
* #param \Auto\EcoleBundle\Entity\Banque $banque
* #return Payement
*/
public function setBanque(\Auto\EcoleBundle\Entity\Banque $banque = null)
{
$this->banque = $banque;
return $this;
}
/**
* Get banque
*
* #return \Auto\EcoleBundle\Entity\Banque
*/
public function getBanque()
{
return $this->banque;
}
/**
* Set rebrique
*
* #param \Auto\EcoleBundle\Entity\PayementRebrique $rebrique
* #return Payement
*/
public function setRebrique(\Auto\EcoleBundle\Entity\PayementRebrique $rebrique = null)
{
$this->rebrique = $rebrique;
return $this;
}
/**
* Get rebrique
*
* #return \Auto\EcoleBundle\Entity\PayementRebrique
*/
public function getRebrique()
{
return $this->rebrique;
}
/**
* Set payementType
*
* #param \Auto\EcoleBundle\Entity\PayementType $payementType
* #return Payement
*/
public function setPayementType(\Auto\EcoleBundle\Entity\PayementType $payementType = null)
{
$this->payementType = $payementType;
return $this;
}
/**
* Get payementType
*
* #return \Auto\EcoleBundle\Entity\PayementType
*/
public function getPayementType()
{
return $this->payementType;
}
/**
* Set client
*
* #param \Auto\EcoleBundle\Entity\Client $client
* #return Payement
*/
public function setClient(\Auto\EcoleBundle\Entity\Client $client = null)
{
$this->client = $client;
return $this;
}
/**
* Get client
*
* #return \Auto\EcoleBundle\Entity\Client
*/
public function getClient()
{
return $this->client;
}
}
I want to use sonata_type_collection in my ClientAdmin:
After adding in Client.php
/**
* #ORM\OneToMany(targetEntity="Auto\EcoleBundle\Entity\Payement", cascade={"persist", "remove"}, mappedBy="client")
*
*/
private $payements;
and
$this->payements = new \Doctrine\Common\Collections\ArrayCollection(); in __constact() function
and updating Payement.php :
/**
* #var \Auto\EcoleBundle\Entity\Client
*
* #ORM\ManyToOne(targetEntity="Auto\EcoleBundle\Entity\Client", inversedBy="payements")
*/
private $client;
then after runing :
php app/console doctrine:generate:entities AutoEcoleBundle:Client
php app/console doctrine:generate:entities AutoEcoleBundle:Payement
to update my getting and setting
BUT the gets and sets of payement not generate in client class
Are you sure that's the right path to your entities ?
php app/console doctrine:generate:entities AutoEcoleBundle:Client
php app/console doctrine:generate:entities AutoEcoleBundle:Payement
You should get an error after those if it's not the right path.
Try adding the right one (Check it in your folders, not sure if it's right):
php app/console doctrine:generate:entities Auto\EcoleBundle\Entity\Client
php app/console doctrine:generate:entities Auto\EcoleBundle\Entity\Payement
resolved by running before generate entities
php app/console doctrine:cache:clear-metadata
any one can explain whay ?
I'm trying to get results from my table using limit and offsett from my table, and this table has a relations with another two tables.. one of them is MayToOne and the other is OneToOne
If I don't use setFirstResult and setMaxResults the results are the expected because return the information of my table and the information of the other tables related but using setFirst.. and setMax.. only return the information of the table and not the information of the tables related.
$query = $this->repository->createQueryBuilder('p')
->where('LOWER(p.name) LIKE LOWER(:term)')
->orWhere('LOWER(p.summary) LIKE LOWER(:term)')
->orwhere('LOWER(p.description) LIKE LOWER(:term)')
->setParameter('term', '%'.$parameters['term'].'%')
->setFirstResult($offset)
->setMaxResults($limit)
->orderBy('p.id','DESC')
->getQuery();
$attractions = $query->getResult();
And I'm trying with paginator using this query:
$query = $this->repository->createQueryBuilder('p')
->where('LOWER(p.name) LIKE LOWER(:term)')
->orWhere('LOWER(p.summary) LIKE LOWER(:term)')
->orwhere('LOWER(p.description) LIKE LOWER(:term)')
->setParameter('term', '%'.$parameters['term'].'%')
->setFirstResult($offset)
->setMaxResults($limit)
->orderBy('p.id','DESC');
$attractions = new Paginator($query, $fetchJoinCollection = true);
But is not working because appear this error:
[Semantical Error] The annotation "#Enum" in property Doctrine\ORM\Mapping\GeneratedValue::$strategy was never imported. Did you maybe forget to add a "use" statement for this annotation?
Entity
<?php
namespace Acme\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\MyBundle\Model\AttractionInterface;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer;
/**
* Attraction
*
* #ORM\Table(name="attractions")
* #ORM\Entity
*
* #ExclusionPolicy("all")
*/
class Attraction implements AttractionInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Expose
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=150, nullable=false)
* #Expose
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="latitude", type="string", length=30, nullable=false)
* #Expose
*/
private $latitude;
/**
* #var string
*
* #ORM\Column(name="longitude", type="string", length=30, nullable=false)
* #Expose
*/
private $longitude;
/**
* #var string
*
* #ORM\Column(name="summary", type="text", nullable=false)
* #Expose
*/
private $summary;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
* #Expose
*/
private $description;
/**
* #var integer
*
* #ORM\Column(name="available_places", type="smallint")
* #Expose
*/
private $availablePlaces;
/**
* #var float
*
* #ORM\Column(name="original_price", type="float", nullable=false)
* #Expose
*/
private $originalPrice;
/**
* #var float
*
* #ORM\Column(name="new_price", type="float", nullable=false)
* #Expose
*/
private $newPrice;
/**
* #var \DateTime
*
* #ORM\Column(name="starting_point", type="datetime", nullable=false)
* #Expose
*/
private $startingPoint;
/**
* #var string
*
* #ORM\Column(name="picture", type="string", length=50, nullable=false)
* #Expose
*/
private $picture;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="Vendor")
* #ORM\JoinColumn(name="vendor", referencedColumnName="id")
* #Expose
*/
private $vendor;
/**
* #var integer
*
* #ORM\OneToOne(targetEntity="Category")
* #ORM\JoinColumn(name="category", referencedColumnName="id")
* #Expose
*/
private $category;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"}, nullable=true)
* #Gedmo\Timestampable(on="create")
* #Expose
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"}, nullable=true)
* #Expose
*/
private $updatedAt;
public function __toString()
{
return $this->name;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Attraction
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set latitude
*
* #param string $latitude
* #return Attraction
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param string $longitude
* #return Attraction
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return string
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Set summary
*
* #param string $summary
* #return Attraction
*/
public function setSummary($summary)
{
$this->summary = $summary;
return $this;
}
/**
* Get summary
*
* #return string
*/
public function getSummary()
{
return $this->summary;
}
/**
* Set description
*
* #param string $description
* #return Attraction
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set availablePlaces
*
* #param integer $availablePlaces
* #return Attraction
*/
public function setAvailablePlaces($availablePlaces)
{
$this->availablePlaces = $availablePlaces;
return $this;
}
/**
* Get availablePlaces
*
* #return integer
*/
public function getAvailablePlaces()
{
return $this->availablePlaces;
}
/**
* Set originalPrice
*
* #param float $originalPrice
* #return Attraction
*/
public function setOriginalPrice($originalPrice)
{
$this->originalPrice = $originalPrice;
return $this;
}
/**
* Get originalPrice
*
* #return float
*/
public function getOriginalPrice()
{
return $this->originalPrice;
}
/**
* Set newPrice
*
* #param float $newPrice
* #return Attraction
*/
public function setNewPrice($newPrice)
{
$this->newPrice = $newPrice;
return $this;
}
/**
* Get newPrice
*
* #return float
*/
public function getNewPrice()
{
return $this->newPrice;
}
/**
* Set startingPoint
*
* #param \DateTime $startingPoint
* #return Attraction
*/
public function setStartingPoint($startingPoint)
{
$this->startingPoint = \DateTime::createFromFormat('Y-m-d H:i:s', $startingPoint);
return $this;
}
/**
* Get startingPoint
*
* #return \DateTime
*/
public function getStartingPoint()
{
return $this->startingPoint;
}
/**
* Set picture
*
* #param string $picture
* #return Attraction
*/
public function setPicture($picture)
{
$this->picture = $picture;
return $this;
}
/**
* Get picture
*
* #return string
*/
public function getPicture()
{
return $this->picture;
}
/**
* Set vendor
*
* #param integer $vendor
* #return Attraction
*/
public function setVendor($vendor)
{
$this->vendor = $vendor;
return $this;
}
/**
* Get vendor
*
* #return integer
*/
public function getVendor()
{
return $this->vendor;
}
/**
* Set category
*
* #param integer $category
* #return Attraction
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get categoryId
*
* #return integer
*/
public function getCategory()
{
return $this->category;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Attraction
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Attraction
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}
have you seen https://github.com/schmittjoh/JMSSerializerBundle/issues/380 ?
it seems having the same issue.
a question: doe it work without that complex query? for example with something like:
$query = $this->repository->createQueryBuilder('p')
->orderBy('p.id','DESC')
->getQuery();
$attractions = $query->getResult();
Problem - I want to save the data that is submitted from the form, when i am saving it to MySQL getting error as -
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updated' cannot be null
But I have given the update column the default value as 0000-00-00 00:00:00 , by default it must take the default value.
I am using the "prepersist" technique to add the code before saving it to MySQL.
The following is my "prepersist" code snippet from which the above error got generated-
public function setBeforeInsertData() {
$this->added = new \DateTime();
}
I have used the following code to overcome the error just for the time being I know that its a wrong approach.
public function setBeforeInsertData() {
$this->added = new \DateTime();
$this->updated = new \DateTime();
}
The following is the Entity for the respective table -
<?php
namespace Skerp\InventoryBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* InventoryLocation
*
* #ORM\Table(name="inventory_location")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
*/
class InventoryLocation
{
/**
* #var string
*
* #ORM\Column(name="locName", type="string", length=50, nullable=false)
*/
private $locname;
/**
* #var string
*
* #ORM\Column(name="address1", type="string", length=50, nullable=false)
*/
private $address1;
/**
* #var string
*
* #ORM\Column(name="address2", type="string", length=50, nullable=true)
*/
private $address2;
/**
* #var string
*
* #ORM\Column(name="address3", type="string", length=50, nullable=true)
*/
private $address3;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=40, nullable=false)
*/
private $city;
/**
* #var string
*
* #ORM\Column(name="zipCode", type="string", length=5, nullable=false)
*/
private $zipcode;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=40, nullable=false)
*/
private $state;
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=40, nullable=false)
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="telephone", type="string", length=20, nullable=true)
*/
private $telephone;
/**
* #var string
*
* #ORM\Column(name="fax", type="string", length=30, nullable=true)
*/
private $fax;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=40, nullable=true)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="contactNo", type="string", length=20, nullable=false)
*/
private $contactno;
/**
* #var string
*
* #ORM\Column(name="addedBy", type="string", length=100, nullable=true)
*/
private $addedby;
/**
* #var \DateTime
*
* #ORM\Column(name="added", type="datetime", nullable=true)
*/
private $added;
/**
* #var \DateTime
*
* #ORM\Column(name="updated", type="datetime", nullable=false)
*/
private $updated;
/**
* #var integer
*
* #ORM\Column(name="inventLocId", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $inventlocid;
/**
* Set locname
*
* #param string $locname
* #return InventoryLocation
*/
public function setLocname($locname)
{
$this->locname = $locname;
return $this;
}
/**
* Get locname
*
* #return string
*/
public function getLocname()
{
return $this->locname;
}
/**
* Set address1
*
* #param string $address1
* #return InventoryLocation
*/
public function setAddress1($address1)
{
$this->address1 = $address1;
return $this;
}
/**
* Get address1
*
* #return string
*/
public function getAddress1()
{
return $this->address1;
}
/**
* Set address2
*
* #param string $address2
* #return InventoryLocation
*/
public function setAddress2($address2)
{
$this->address2 = $address2;
return $this;
}
/**
* Get address2
*
* #return string
*/
public function getAddress2()
{
return $this->address2;
}
/**
* Set address3
*
* #param string $address3
* #return InventoryLocation
*/
public function setAddress3($address3)
{
$this->address3 = $address3;
return $this;
}
/**
* Get address3
*
* #return string
*/
public function getAddress3()
{
return $this->address3;
}
/**
* Set city
*
* #param string $city
* #return InventoryLocation
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set zipcode
*
* #param string $zipcode
* #return InventoryLocation
*/
public function setZipcode($zipcode)
{
$this->zipcode = $zipcode;
return $this;
}
/**
* Get zipcode
*
* #return string
*/
public function getZipcode()
{
return $this->zipcode;
}
/**
* Set state
*
* #param string $state
* #return InventoryLocation
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return string
*/
public function getState()
{
return $this->state;
}
/**
* Set country
*
* #param string $country
* #return InventoryLocation
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set telephone
*
* #param string $telephone
* #return InventoryLocation
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
return $this;
}
/**
* Get telephone
*
* #return string
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* Set fax
*
* #param string $fax
* #return InventoryLocation
*/
public function setFax($fax)
{
$this->fax = $fax;
return $this;
}
/**
* Get fax
*
* #return string
*/
public function getFax()
{
return $this->fax;
}
/**
* Set email
*
* #param string $email
* #return InventoryLocation
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set contactno
*
* #param string $contactno
* #return InventoryLocation
*/
public function setContactno($contactno)
{
$this->contactno = $contactno;
return $this;
}
/**
* Get contactno
*
* #return string
*/
public function getContactno()
{
return $this->contactno;
}
/**
* Set addedby
*
* #param string $addedby
* #return InventoryLocation
*/
public function setAddedby($addedby)
{
$this->addedby = $addedby;
return $this;
}
/**
* Get addedby
*
* #return string
*/
public function getAddedby()
{
return $this->addedby;
}
/**
* Set added
*
* #param \DateTime $added
* #return InventoryLocation
*/
public function setAdded($added)
{
$this->added = $added;
return $this;
}
/**
* Get added
*
* #return \DateTime
*/
public function getAdded()
{
return $this->added;
}
/**
* Set updated
*
* #param \DateTime $updated
* #return InventoryLocation
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Get inventlocid
*
* #return integer
*/
public function getInventlocid()
{
return $this->inventlocid;
}
/**
* #var integer
*/
private $id;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* #ORM\PrePersist
*/
public function setBeforeInsertData() {
$this->added = new \DateTime();
//$this->updated = new \DateTime();
}
}
Thanks in Advance.
Your entity class should have following annotation: #ORM\HasLifecycleCallbacks(). Your methods should have #ORM\PrePersist annotation. More info: http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks