Symfony 3 Call to a member function format() on string - php

Hello I´m new on symfony.
I made an entity with doctrine:generate:entity and return me this:
<?php
namespace LoginBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class Users
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="users_id_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=30, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=30, nullable=false)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=200, nullable=false)
*/
private $password;
/**
* #var \DateTime
*
* #ORM\Column(name="ts", type="datetime", nullable=false)
*/
private $ts = 'now()';
/**
* #var string
*
* #ORM\Column(name="mail", type="string", length=150, nullable=false)
*/
private $mail;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Users
*/
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 Users
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* #return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set password
*
* #param string $password
*
* #return Users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set ts
*
* #param \DateTime $ts
*
* #return Users
*/
public function setTs($ts)
{
$this->ts = $ts;
return $this;
}
/**
* Get ts
*
* #return \DateTime
*/
public function getTs()
{
return $this->ts;
}
/**
* Set mail
*
* #param string $mail
*
* #return Users
*/
public function setMail($mail)
{
$this->mail = $mail;
return $this;
}
/**
* Get mail
*
* #return string
*/
public function getMail()
{
return $this->mail;
}
}
The problem is the line with the timestamp
private $ts = 'now()';
And now i´m getting this error
Call to a member function format() on string
in vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\DateTimeType.php (line 53)
public function convertToDatabaseValue($value, AbstractPlatform $platform) { return ($value !== null) ? $value->format($platform->getDateTimeFormatString()) : null; }
Thanks in advance

If you want to get the current date and time, you need to set current dateTime. You need create __construct method:
public function __construct()
{
$this->setTs(new \DateTime());
}

Related

replace the authentication layer and user entity in sylius

I have created a symfony2 project and custom Authentication Provider (http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html) is used. my new task is to create an E-commerce application in sylius which should work alongside with my current symfony2 project. i want to make my current user table in symfony2 project as user provider in sylius...is it possible to create such project..... since fosUserBundle and fosOauthServer bundel are installed in sylius how can i override these bundles with my custom authentication mechanism
My symfony2 project configurations are mentioned below
I tried the following in security.yml
security:
encoders:
AppBundle\Entity\Users:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: AppBundle:Users
api_key_user_provider:
id: api_key_user_provider
firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false
api:
pattern: ^/api
stateless: true
simple_preauth:
authenticator: apikey_authenticator
provider: api_key_user_provider
web:
anonymous: ~
http_basic: ~
provider: our_db_provider
form_login:
login_path: /login
check_path: /login_check
this is my user class
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Users
*
* #ORM\Table(name="users", uniqueConstraints= {#ORM\UniqueConstraint(name="users_user_name_unique", columns={"user_name"}), #ORM\UniqueConstraint(name="users_xmpp_password_unique", columns= {"xmpp_password"})})
* #ORM\Entity(repositoryClass="AppBundle\Entity\UsersRepository")
*/
class Users implements UserInterface, \Serializable {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="parent_id", type="integer", nullable=false)
*/
private $parentId;
/**
* #var string
*
* #ORM\Column(name="first_name", type="string", length=100, nullable=false)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="last_name", type="string", length=100, nullable=false)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="user_name", type="string", length=120, nullable=false)
*/
private $userName;
/**
* #var string
*
* #ORM\Column(name="reg_type", type="string", length=20, nullable=false)
*/
private $regType;
/**
* #var string
*
* #ORM\Column(name="oauth_uid", type="string", length=255, nullable=false)
*/
private $oauthUid;
/**
* #var boolean
*
* #ORM\Column(name="active", type="boolean", nullable=false)
*/
private $active;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=100, nullable=false)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=150, nullable=false)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="xmpp_password", type="string", length=20, nullable=false)
*/
private $xmppPassword;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt = '0000-00-00 00:00:00';
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt = '0000-00-00 00:00:00';
/**
* #var \DateTime
*
* #ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private $deletedAt;
/**
* #var string
*
* #ORM\Column(name="activation_code", type="string", length=50, nullable=true)
*/
private $activationCode;
/**
* #var string
*
* #ORM\Column(name="user_profile_pic", type="string", length=200, nullable=false)
*/
private $userProfilePic = 'uploads/defaults/user/profile_pic.jpg';
/**
* #var string
*
* #ORM\Column(name="user_timeline_pic", type="string", length=200, nullable=false)
*/
private $userTimelinePic = 'uploads/defaults/user/timeline_pic.jpg';
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=50, nullable=false)
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=50, nullable=false)
*/
private $state;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=50, nullable=false)
*/
private $city;
/**
* #var string
*
* #ORM\Column(name="hobbies", type="string", length=100, nullable=false)
*/
private $hobbies;
/**
* #var string
*
* #ORM\Column(name="interests", type="string", length=100, nullable=false)
*/
private $interests;
/**
* #var string
*
* #ORM\Column(name="about", type="string", length=500, nullable=false)
*/
private $about;
/**
* #var boolean
*
* #ORM\Column(name="gender", type="boolean", nullable=false)
*/
private $gender;
/**
* #var \DateTime
*
* #ORM\Column(name="dob", type="date", nullable=false)
*/
private $dob;
/**
* #var integer
*
* #ORM\Column(name="quickblox_id", type="integer", nullable=false)
*/
private $quickbloxId;
/**
* #var boolean
*
* #ORM\Column(name="privacy", type="boolean", nullable=false)
*/
private $privacy = '0';
/**
* #var string
*
* #ORM\Column(name="school", type="string", length=255, nullable=false)
*/
private $school;
/**
* #var string
*
* #ORM\Column(name="college", type="string", length=255, nullable=false)
*/
private $college;
/**
* #var string
*
* #ORM\Column(name="work", type="string", length=255, nullable=false)
*/
private $work;
/**
* #var string
*
* #ORM\Column(name="relationship_status", type="string", length=50, nullable=false)
*/
private $relationshipStatus;
public function getSalt() {
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getRoles() {
return array('ROLE_API');
}
public function eraseCredentials() {
}
/** #see \Serializable::serialize() */
public function serialize() {
return serialize(array(
$this->id,
$this->email,
// see section on salt below
// $this->salt,
));
}
/** #see \Serializable::unserialize() */
public function unserialize($serialized) {
list (
$this->id,
$this->email,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set parentId
*
* #param integer $parentId
* #return Users
*/
public function setParentId($parentId) {
$this->parentId = $parentId;
return $this;
}
/**
* Get parentId
*
* #return integer
*/
public function getParentId() {
return $this->parentId;
}
/**
* Set firstName
*
* #param string $firstName
* #return Users
*/
public function setFirstName($firstName) {
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName() {
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
* #return Users
*/
public function setLastName($lastName) {
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* Set userName
*
* #param string $userName
* #return Users
*/
public function setUserName($userName) {
$this->userName = $userName;
return $this;
}
/**
* Get userName
*
* #return string
*/
public function getUserName() {
return $this->email;
}
/**
* Set regType
*
* #param string $regType
* #return Users
*/
public function setRegType($regType) {
$this->regType = $regType;
return $this;
}
/**
* Get regType
*
* #return string
*/
public function getRegType() {
return $this->regType;
}
/**
* Set oauthUid
*
* #param string $oauthUid
* #return Users
*/
public function setOauthUid($oauthUid) {
$this->oauthUid = $oauthUid;
return $this;
}
/**
* Get oauthUid
*
* #return string
*/
public function getOauthUid() {
return $this->oauthUid;
}
/**
* Set active
*
* #param boolean $active
* #return Users
*/
public function setActive($active) {
$this->active = $active;
return $this;
}
/**
* Get active
*
* #return boolean
*/
public function getActive() {
return $this->active;
}
/**
* Set email
*
* #param string $email
* #return Users
*/
public function setEmail($email) {
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail() {
return $this->email;
}
/**
* Set password
*
* #param string $password
* #return Users
*/
public function setPassword($password) {
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword() {
return $this->password;
}
/**
* Set xmppPassword
*
* #param string $xmppPassword
* #return Users
*/
public function setXmppPassword($xmppPassword) {
$this->xmppPassword = $xmppPassword;
return $this;
}
/**
* Get xmppPassword
*
* #return string
*/
public function getXmppPassword() {
return $this->xmppPassword;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Users
*/
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 Users
*/
public function setUpdatedAt($updatedAt) {
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt() {
return $this->updatedAt;
}
/**
* Set deletedAt
*
* #param \DateTime $deletedAt
* #return Users
*/
public function setDeletedAt($deletedAt) {
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get deletedAt
*
* #return \DateTime
*/
public function getDeletedAt() {
return $this->deletedAt;
}
/**
* Set activationCode
*
* #param string $activationCode
* #return Users
*/
public function setActivationCode($activationCode) {
$this->activationCode = $activationCode;
return $this;
}
/**
* Get activationCode
*
* #return string
*/
public function getActivationCode() {
return $this->activationCode;
}
/**
* Set userProfilePic
*
* #param string $userProfilePic
* #return Users
*/
public function setUserProfilePic($userProfilePic) {
$this->userProfilePic = $userProfilePic;
return $this;
}
/**
* Get userProfilePic
*
* #return string
*/
public function getUserProfilePic() {
return $this->userProfilePic;
}
/**
* Set userTimelinePic
*
* #param string $userTimelinePic
* #return Users
*/
public function setUserTimelinePic($userTimelinePic) {
$this->userTimelinePic = $userTimelinePic;
return $this;
}
/**
* Get userTimelinePic
*
* #return string
*/
public function getUserTimelinePic() {
return $this->userTimelinePic;
}
/**
* Set country
*
* #param string $country
* #return Users
*/
public function setCountry($country) {
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry() {
return $this->country;
}
/**
* Set state
*
* #param string $state
* #return Users
*/
public function setState($state) {
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return string
*/
public function getState() {
return $this->state;
}
/**
* Set city
*
* #param string $city
* #return Users
*/
public function setCity($city) {
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity() {
return $this->city;
}
/**
* Set hobbies
*
* #param string $hobbies
* #return Users
*/
public function setHobbies($hobbies) {
$this->hobbies = $hobbies;
return $this;
}
/**
* Get hobbies
*
* #return string
*/
public function getHobbies() {
return $this->hobbies;
}
/**
* Set interests
*
* #param string $interests
* #return Users
*/
public function setInterests($interests) {
$this->interests = $interests;
return $this;
}
/**
* Get interests
*
* #return string
*/
public function getInterests() {
return $this->interests;
}
/**
* Set about
*
* #param string $about
* #return Users
*/
public function setAbout($about) {
$this->about = $about;
return $this;
}
/**
* Get about
*
* #return string
*/
public function getAbout() {
return $this->about;
}
/**
* Set gender
*
* #param boolean $gender
* #return Users
*/
public function setGender($gender) {
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* #return boolean
*/
public function getGender() {
return $this->gender;
}
/**
* Set dob
*
* #param \DateTime $dob
* #return Users
*/
public function setDob($dob) {
$this->dob = $dob;
return $this;
}
/**
* Get dob
*
* #return \DateTime
*/
public function getDob() {
return $this->dob;
}
/**
* Set quickbloxId
*
* #param integer $quickbloxId
* #return Users
*/
public function setQuickbloxId($quickbloxId) {
$this->quickbloxId = $quickbloxId;
return $this;
}
/**
* Get quickbloxId
*
* #return integer
*/
public function getQuickbloxId() {
return $this->quickbloxId;
}
/**
* Set privacy
*
* #param boolean $privacy
* #return Users
*/
public function setPrivacy($privacy) {
$this->privacy = $privacy;
return $this;
}
/**
* Get privacy
*
* #return boolean
*/
public function getPrivacy() {
return $this->privacy;
}
/**
* Set school
*
* #param string $school
* #return Users
*/
public function setSchool($school) {
$this->school = $school;
return $this;
}
/**
* Get school
*
* #return string
*/
public function getSchool() {
return $this->school;
}
/**
* Set college
*
* #param string $college
* #return Users
*/
public function setCollege($college) {
$this->college = $college;
return $this;
}
/**
* Get college
*
* #return string
*/
public function getCollege() {
return $this->college;
}
/**
* Set work
*
* #param string $work
* #return Users
*/
public function setWork($work) {
$this->work = $work;
return $this;
}
/**
* Get work
*
* #return string
*/
public function getWork() {
return $this->work;
}
/**
* Set relationshipStatus
*
* #param string $relationshipStatus
* #return Users
*/
public function setRelationshipStatus($relationshipStatus) {
$this->relationshipStatus = $relationshipStatus;
return $this;
}
/**
* Get relationshipStatus
*
* #return string
*/
public function getRelationshipStatus() {
return $this->relationshipStatus;
}
You can find the answer here https://github.com/Sylius/Sylius/issues/2931.

While Inserting the data in MySQL getting error for the update column in Symfony2.4, Doctrine, PHP

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

Updating a field in a table from a join OneToOne Symfony2 / Doctrine 2

I have a problem with Doctrine2 in Symfony2 and two relationed entities.
I have a class Users
class Users
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=50)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=50)
*/
private $password;
/**
* #var boolean
*
* #ORM\Column(name="type", type="boolean")
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="first_name", type="string", length=100)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="last_name", type="string", length=100)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="tel", type="string", length=255)
*/
private $tel;
/**
* #var string
*
* #ORM\Column(name="url_img", type="string", length=255)
*/
private $urlImg;
/**
* #var string
*
* #ORM\Column(name="company", type="string", length=255)
*/
private $company;
/**
* #ORM\OneToOne(targetEntity="Providers", mappedBy="userId", cascade={"persist"})
*/
protected $provider;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* #param string $username
* #return Users
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* #param string $password
* #return Users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set type
*
* #param boolean $type
* #return Users
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return boolean
*/
public function getType()
{
return $this->type;
}
/**
* Set idPrest
*
* #param integer $idPrest
* #return Users
*/
public function setIdPrest($idPrest)
{
$this->idPrest = $idPrest;
return $this;
}
/**
* Get idPrest
*
* #return integer
*/
public function getIdPrest()
{
return $this->idPrest;
}
/**
* Set idEmbaucher
*
* #param integer $idEmbaucher
* #return Users
*/
public function setIdEmbaucher($idEmbaucher)
{
$this->idEmbaucher = $idEmbaucher;
return $this;
}
/**
* Get idEmbaucher
*
* #return integer
*/
public function getIdEmbaucher()
{
return $this->idEmbaucher;
}
/**
* Set email
*
* #param string $email
* #return Users
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set firstName
*
* #param string $firstName
* #return Users
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
* #return Users
*/
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 Users
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return string
*/
public function getTel()
{
return $this->tel;
}
/**
* Set urlImg
*
* #param string $urlImg
* #return Users
*/
public function setUrlImg($urlImg)
{
$this->urlImg = $urlImg;
return $this;
}
/**
* Get urlImg
*
* #return string
*/
public function getUrlImg()
{
return $this->urlImg;
}
/**
* Set company
*
* #param string $company
* #return Users
*/
public function setCompany($company)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* #return string
*/
public function getCompany()
{
return $this->company;
}
/**
* Set provider
*
* #param Providers $provider
* #return Users
*/
public function setProvider($provider = null)
{
$this->provider = $provider;
return $this;
}
/**
* Get provider
*
* #return Providers
*/
public function getProvider()
{
return $this->provider;
}
}
in relation with class provider on OneToOne (inheriting)
class Providers
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Users", inversedBy="provider")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $userId;
/**
* #ORM\OneToOne(targetEntity="Advertisement", mappedBy="provider")
*/
protected $advertisement;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* #param \leBonCoinDesServices\MainBundle\Entity\Users $userId
* #return Providers
*/
public function setUserId($userId = null)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* #return \leBonCoinDesServices\MainBundle\Entity\Users
*/
public function getUserId()
{
return $this->userId;
}
}
I just want to get the fields user_id created from the relationship for be able to when I create a user get the id of this one and give it to the user_id field.
And when I test my controller to create a user and a provider :
public function newUserAction()
{
$user = new Users();
$user->setUsername("gokusan");
$user->setPassword("test");
$user->setType("p");
$user->setEmail("test#msn.com");
$user->setFirstName("KHALIL");
$user->setLastName("Ahmed");
$user->setTel("0142021148");
$user->setUrlImg("img/test/");
$user->setCompany("Push&Pull");
$this->getDoctrine()->getEntityManager()->persist($user);
$this->getDoctrine()->getEntityManager()->flush();
$provider = new Providers();
$id = $user->getId();
$provider->setUserId($id);
$this->getDoctrine()->getEntityManager()->persist($provider);
$this->getDoctrine()->getEntityManager()->flush();
die("User Added");
}
that puts me error :
Warning: spl_object_hash() expects parameter 1 to be object, integer given in F:\Programs Files\Program Files\wamp\www\leboncoindesservices\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1367
Your Providers::setUserId($userId) expects $userId to be of type \leBonCoinDesServices\MainBundle\Entity\Users and not of type integer. So you should be able to pass your Users object to the $provider like
$user = new Users();
...
$provder = new Providers();
...
$provider->setUserId($user);

Generating Getters and setters Symfony2

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.

Symfony 2: with passwords encryption

$factory = $this->get('security.encoder_factory');
var_dump($factory);
$encoder = $factory->getEncoder($user);
//$encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
$password = $encoder->encodePassword($user->getUserPass(), $user->getUserSalt());
$user->setUserPass($password);
The error
Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Core\Encoder\EncoderFactory::getEncoder() must implement interface Symfony\Component\Security\Core\User\UserInterface, instance of Acme\AdminBundle\Entity\SyjUsers given, called in D:\Zend\joke\src\Acme\AdminBundle\Controller\AdminController.php on line 28 and defined in D:\Zend\joke\vendor\symfony\src\Symfony\Component\Security\Core\Encoder\EncoderFactory.php line 33
500 Internal Server Error - ErrorException
The user entity
<?php
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Acme\UserBundle\Entity\SyjUsers
*
* #ORM\Table(name="syj_users")
*
* #ORM\Entity
*/
class SyjUsers implements UserInterface
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
*
* #ORM\Id
*
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $userLogin
*
* #ORM\Column(name="user_login", type="string", length=60, nullable=false)
*/
private $userLogin;
/**
* #var string $userSalt
*
* #ORM\Column(name="user_salt", type="string", length=64, nullable=false)
*/
private $userSalt;
/**
* #var string $userPass
*
* #ORM\Column(name="user_pass", type="string", length=64, nullable=false)
*/
private $userPass;
/**
* #var string $userNicename
*
* #ORM\Column(name="user_nicename", type="string", length=60, nullable=true)
*/
private $userNicename;
/**
* #var string $userEmail
*
* #ORM\Column(name="user_email", type="string", length=100, nullable=true)
*/
private $userEmail;
/**
* #var string $userUrl
*
* #ORM\Column(name="user_url", type="string", length=60, nullable=true)
*/
private $userUrl;
/**
* #var boolean $userStatus
*
* #ORM\Column(name="user_status", type="boolean", nullable=false)
*/
private $userStatus;
/**
* #var string $userDisplayName
*
* #ORM\Column(name="user_display_name", type="string", length=60, nullable=true)
*/
private $userDisplayName;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userLogin
*
* #param string $userLogin
*/
public function setUserLogin($userLogin)
{
$this->userLogin = $userLogin;
}
/**
* Get userLogin
*
* #return string
*/
public function getUserLogin()
{
return $this->userLogin;
}
/**
* Set userSalt
*
* #param string $userSalt
*/
public function setUserSalt($userSalt)
{
$this->userSalt = $userSalt;
}
/**
* Get userSalt
*
* #return string
*/
public function getUserSalt()
{
return $this->userSalt;
}
/**
* Set userPass
*
* #param string $userPass
*/
public function setUserPass($userPass)
{
$this->userPass = $userPass;
}
/**
* Get userPass
*
* #return string
*/
public function getUserPass()
{
return $this->userPass;
}
/**
* Set userNicename
*
* #param string $userNicename
*/
public function setUserNicename($userNicename)
{
$this->userNicename = $userNicename;
}
/**
* Get userNicename
*
* #return string
*/
public function getUserNicename()
{
return $this->userNicename;
}
/**
* Set userEmail
*
* #param string $userEmail
*/
public function setUserEmail($userEmail)
{
$this->userEmail = $userEmail;
}
/**
* Get userEmail
*
* #return string
*/
public function getUserEmail()
{
return $this->userEmail;
}
/**
* Set userUrl
*
* #param string $userUrl
*/
public function setUserUrl($userUrl)
{
$this->userUrl = $userUrl;
}
/**
* Get userUrl
*
* #return string
*/
public function getUserUrl()
{
return $this->userUrl;
}
/**
* Set userStatus
*
* #param boolean $userStatus
*/
public function setUserStatus($userStatus)
{
$this->userStatus = $userStatus;
}
/**
* Get userStatus
*
* #return boolean
*/
public function getUserStatus()
{
return $this->userStatus;
}
/**
* Set userDisplayName
*
* #param string $userDisplayName
*/
public function setUserDisplayName($userDisplayName)
{
$this->userDisplayName = $userDisplayName;
}
/**
* Get userDisplayName
*
* #return string
*/
public function getUserDisplayName()
{
return $this->userDisplayName;
}
/**
* #inheritDoc
*/
public function getUsername()
{
return $this->userLogin;
}
/**
* #inheritDoc
*/
public function getSalt()
{
return $this->userSalt;
}
/**
* #inheritDoc
*/
public function getPassword()
{
return $this->userPass;
}
/**
* #inheritDoc
*/
public function getRoles()
{
return array('ROLE_USER');
}
/**
* #inheritDoc
*/
public function eraseCredentials()
{
}
/**
* #inheritDoc
*/
public function equals(UserInterface $user)
{
return $this->userLogin === $user->getUsername();
}
}
Have you tryed to
implements CustomUserInterface
instead of the
implements UserInterface

Categories