I have two entities one is main and second is additional, they joined as OneToOne. I won't show all, i think it's not necessary:
apiKey
/**
* #ORM\OneToOne(targetEntity="Eve\ApiBundle\Entity\Account\apiKeyInfo", inversedBy="apiKey_byKeyID")
* #ORM\JoinColumn(name="keyID", referencedColumnName="keyID")
*/
private $apiKeyInfo_byKeyID;
public function get_apiKeyInfo_byKeyID()
{
return $this->apiKeyInfo_byKeyID;
}
apiKeyInfo
/**
* #ORM\OneToOne(targetEntity="Eve\ProfileBundle\Entity\apiKey", mappedBy="apiKeyInfo_byKeyID")
*/
private $apiKey_byKeyID;
public function get_apiKey_byKeyID()
{
return $this->apiKey_byKeyID;
}
/**
* #ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* #param string $type
* #return apiKeyInfo
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* #return string
*/
public function getType()
{
return $this->type;
}
Relation i call in twig (apiKey is goten in php part)
apiKey.get_apiKeyInfo_byKeyID.type
It works fine when db tables is full of data, but when "apiKeyInfo" table doesn't have same keyID it throw me exception:
Entity was not found.
I understand why, because it cannot find entries with same keyID ... But i don't know how to deal with it.
So question is...
How can i make result, of this relation, can be null?
Trouble was in incorrect descriptions of entity. Problem solved.
Related
I have a problem of recognition constrains() method in Symfony2. I have a relationship between the groups & Roles entities: So a group must have a mandatory role and the role may or may not have one or more groups. So in my addRoles function ( Groups $grp) I have checked each time if the group has a role so we joust if not assigning a role. But when inserting,
I encounter a problem:
PHP Fatal error: Call to undefined method
MemberShipManagement\GroupsBundle\Entity\Roles::contains() in
/var/www/Project_Console/src/MemberShipManagement/GroupsBundle/Entity/Roles.php
on line 118,
Class Groups:
/**
* #var Roles $role
*
* #ORM\ManyToOne(targetEntity="Roles", inversedBy="groups")
* #ORM\JoinColumn(name="role_id", referencedColumnName="id", nullable=false)
*
* #Assert\Valid()
*/
protected $role;
Class Roles:
/**
* #var ArrayCollection $groups
*
* #ORM\OneToMany(targetEntity="Groups", mappedBy="role", cascade={"remove"} )
*#Assert\Valid()
*/
protected $groups;
/**
* Add group
* #param Groups $grp
*/
public function addRoles(Groups $grp) {
// $grp->setRole($this);
if (!$this->groups->contains($grp)) {
$this->groups->add($grp);
}
return $this;
}
/**
* Remove groups
* #param Groups $groups
*/
public function removeRoles(Groups $groups)
{
if ($this->groups->contains($groups)) {
$this->groups->removeElement($groups);
}
return $this;
}
public function __construct()
{
$this->groups = new ArrayCollection();
}
thank you :)
Maybe you could try to check if the data exists before calling the contains method like,
public function addRoles(Groups $grp) {
// $grp->setRole($this);
if ( !isEmpty($this->groups) ){
if (!$this->groups->contains($grp)) {
$this->groups->add($grp);
}
}
return $this;
}
since the error you get seems to be derived from the fact that $groups are not interpreted as ArrayCollection defined in doctrine.(if so, you could've called contains method defined there)
http://www.doctrine-project.org/api/common/2.4/class-Doctrine.Common.Collections.ArrayCollection.html
As long as other examples using contains method are found like this(https://groups.google.com/forum/#!topic/doctrine-user/i6IhBPHALkk) your approach looks correct.
Otherwise you could try to change $role to private instead of protected?
I have a entity :
/**
* #ORM\Entity
* #ORM\Table(name="organization")
*/
class Organization
{
/*
*#ORM\OneToMany(targetEntity="Link" , mappedBy="organization")
*/
private $links;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Organization
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set site
*
* #param string $site
* #return Organization
*/
public function setSite($site)
{
$this->site = $site;
return $this;
}
/**
* Get site
*
* #return string
*/
public function getSite()
{
return $this->site;
}
public function __construct()
{
$this->links = new ArrayCollection();
}
}
when run command :
php console doctrine:generate:entities News/VillageNewsBundle/Entity/Organization
when i run this command all the getter and setter
will generated but getter and setter for relation not add to my entity?!
That's because your OneToMany annotation is wrong.
You must use the correct doc synthax, which is:
/**
* [...]
*/
Here, it misses one '*' before your OneToMany annotation. Add it and it will work. :-)
I create a database and I had the entity file, I added a new column to my table, now how can I update the Entity class and add getter ad setter to this new element ?
the table contains ; userid, username, firstname, password
I added a column "admin" (boolean)
here is my class : Users:
<?php
namespace Login\LoginBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Redirect
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Login\LoginBundle\Entity\Users")
*/
class Users
{
/**
* #var string
*/
private $userName;
/**
* #var string
*/
private $firstName;
/**
* #var string
*/
private $password;
/**
* #var integer
*/
private $userid;
/**
* 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 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 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;
}
/**
* Get userid
*
* #return integer
*/
public function getUserid()
{
return $this->userid;
}
}
The generator bundle gives you a command to generate an entity from a set of fields via the app/console doctrine:generate:entity command.
Internally, this works by using template files to create your new entities, based on your inputs. Unfortunately, the tool does not yet have the ability to modify existing classes.
If you'd like, you can request that feature here: https://github.com/sensiolabs/SensioGeneratorBundle
But in the meantime, your best bet is one of the following:
If you haven't modified anything, you could erase the file and re-create it using app/console doctrine:generate:entity.
You can just add the field yourself, along with the getters, setters, and Doctrine configuration, using the formats you see used for the other fields in that class (this is probably the easier way, to be honest).
I have an Entity i.e Users. I want to make getters and setters of this entity in Doctrine, so that Doctrine can read it.
How can I do it, can someone provide me basic example? I am a beginner
How to insert data in this database table?
Here is my Users entity
<?php
/**
* #Entity
* #Table(name="users")
* Total Number of Columns : 32
*/
class Users{
/* Attributes of Users */
/**
* #Id
* #Column(type="integer")
* #GeneratedValue
* #dummy
* #Assert\NotEmpty
*/
private $id;
/**
* #Column(type="string")
* #Assert\NotEmpty
*/
private $name;
/**
* #Column(type="string")
* #Assert\NotEmpty
*/
private $email;
}
?>
Try with this command:
php app/console doctrine:generate:entities YourBundle:YourEntity
For example, if you wanted to have a setter for your email property, you would do:
public function setEmail($email)
{
$this->email = $email;
return $this;
}
public function getEmail()
{
return $this->email;
}
The first is the setter (it sets the value of email on the object) and the second is the getter (it gets the value of email from the object). Hope that helps :)
You can use magic methods if you're lazy enough not to define your own methods for each property.
public function __get($property)
{
return $this->$property;
}
public function __set($property,$value)
{
$this->$property = $value;
}
It's better to create a method for each property though
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
Have a look at the answers here Doctrine 2 Whats the Recommended Way to Access Properties?
Simple example, we've got
/**
* #ORM\Column(name="api_keyID", type="integer", nullable=false)
*/
private $api_keyID;
/**
* #return integer
*/
public function getApi_keyID()
{
return $this->api_keyID;
}
/**
* #param integer $api_keyID
* #return object
*/
public function setApi_keyID($data)
{
$this->api_keyID = $data;
return $this;
}
Look at method name and column name. When i try
//...
->findOneByApi_keyID($some);
I'm getting an error like
Entity 'entity\path' has no field 'apiKeyID'. You can therefore not call 'findOneByApi_keyID' on the entities' repository
So doctrine\symfony eats underscore? О.о And i cannot use it in column name?
is the way out
$repository->findBy(array('is_enabled' => true));
Founded here
Magic Doctrine2 finders when field has underscore?