I have one table for which I am creating two entities.
One entity is used in my one core bundle which is used for many projects.
For new project I am creating new bundle and I want to add one more column in that entity.
Can anyone please guide me how can I override entity in other bundle.
You can extend your original User entity to something like this.
namespace XXXX;
use Doctrine\ORM\Mapping as ORM;
use XXXXX as BaseUser;
/**
* #ORM\Entity
* #ORM\Table(name="User")
*/
class User extends BaseUser
{
/**
* #var string
*
* #ORM\Column(name="newProperty", type="string", nullable=false)
*/
private $newProperty;
.....
.....
}
This will have all properties from your class BaseUser and you can have additional properties in this new class that you are trying to create. You can add set and get methods here too.
Related
I'm using the SyliusResourceBundle as a standalone package for exposing data through an API.
When i request entities that have relationships with some other entities, i always get a full response with all the related entities properties included. This leads to heavy JSON responses, and too much data to download on the client side.
Typically, if my entity has a $user property like this :
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User", inversedBy="object")
*/
private $user;
I get all the user's stuff in the API response when i request the object : name, email, etc.
Is there a way to only get a list of properties/entities i need ? Like with an annotation or something ?
Thanks
For the record, SyliusResourceBundle uses JMSSeriliazerBundle, so it was just a matter of exclusion policy in the Resource Entity.
I just had to exclude all fields at the Entity level, and only expose the field i needed like this :
namespace AppBundle\Entity;
use JMS\Serializer\Annotation as JMS;
use Sylius\Component\Resource\Model\ResourceInterface;
use Doctrine\ORM\Mapping as ORM;
/**
*
* #JMS\ExclusionPolicy("all")
*/
class MyResource implements ResourceInterface
{
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="object")
*/
private $user;
/**
* #ORM\Column(type="string")
* #JMS\Expose()
*/
private $name;
}
See doc here.
I am developing an application with symfony 2.8 with the bundle "FOSUSERBUNDLE", my problem is when I try to register a user, I can see the registration form, but when registering I throw the following error:
Unable to find the object manager associated with an entity of class
"Alienigena\ViviendaBundle\Entity\User".
I follow step by step the official tutorial of "FOSUSERBUNDLE", my USER class is:
namespace Alienigena\ViviendaBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
My problem originated because at the moment of mapping the bd with reverse engineering I did with the mapping in xml, I should have mapped it with annotations.
the solution consisted in eliminating the files with extension xml that were generated with the inverse engineering.
Thanks
I'm learning how to use FOSuserBundle and I've finished configuring it following the steps from the Symfony docs : Getting Started With FOSUserBundle
The problem is that when I want to update the database schema (doctrine:schema:update --force) I get :
Nothing to update - your database is already in sync with the current entity metadata.
But I think that it should update the user table.
This is my User class :
namespace Stage\AdminBundle\Bundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
So when I try to login :"Authentication request could not be processed due to a system problem.
" appears.
I don't know what should I do , please help me
thanks
finally i've found the solution , it's a mistake in the namespace
it's :: namespace Stage\AdminBundle\Bundle\Entity;
but it should be namespace Stage\AdminBundle\Entity;
What is the debugger trying to tell me from that red notification in the toolbar?
When i clicked it, it showed me some papping errors. Why is it an error?
Category Entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Category
*
* #ORM\Table(name="ewaste_category")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Category
{
/**
* #ORM\OneToMany(targetEntity="Type", mappedBy="category")
*/
protected $type;
}
Model Entity
class Model
{
/**
* #ORM\ManyToOne(targetEntity="Type", inversedBy="model")
* #ORM\JoinColumn(name="model_id", referencedColumnName="id")
*/
protected $type;
}
Type entity
class Type
{
/**
* #ORM\ManyToOne(targetEntity="Category", inversedBy="type")
* #ORM\JoinColumn(name="type_id", referencedColumnName="id")
*/
protected $category;
/**
* #ORM\OneToMany(targetEntity="Model", mappedBy="model")
*/
protected $model;
}
Solved the problem.
I changed the $type to $model in Model Entity
You should check your doctrine mapping configuration specialy indexedBy and mappedBy properties
You have an entity named Type that has a relationship with another entity named Model.
Somewhere in Type you have an association that tries to map to a field that should be into Model entity (named model itself) that isn't there. This is first error you're seeing here.
Second error talks about Model entity. It says that the association between Category and Model and Model And Type are not consistent, so you're using some fields for mappings that aren't correct.
Could you paste those three entities so we can provide you a solution also?
I have 2 Symfony bundles. AdminBundle will always be installed. PageBundle may or may not be installed.
I want to define a base Entity called AdminModule (name, controller class, description, enabled), and also a PageModule which simply inherits from AdminModule ( the entities controller will implement a specific interface).
<?php
namespace AdminBundle\Entity;
/**
* Admin Component
*
* #ORM\Entity
* #ORM\Table(name="admin_module")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"page" = "\PageBundle\Entity\PageComponent"})
*/
class AdminModule
{
// private vars, getters, setters
}
?>
<?php
namespace PageBundle\Entity;
use AdminBundle\Entity\AdminModule;
/**
* Page Component
*
* #ORM\Entity
* #ORM\Table(name="page_module")
*/
class PageModule extends AdminModule
{
//
}
?>
The issue I have, I think, is that the AdminModule annotation #ORM\DiscriminatorMap({"page" = "\PageBundle\Entity\PageModule"}) requires definition on the AdminBundle - but the PageBundle may not be installed.
I believe must have the wrong type of inheritance structure (?) however I am not clear on what alternative approaches I can take? Thanks for any help or direction :)
you can't do what you're trying to with table inheritance mappings,
because you have to write annotations in the parent class, so the parent class itself ends up being coupled with his children.
what you could use is a mapped superclass (#MappedSuperclass) to extend the actual parent entities from.
all your common properties should then go into the mapped superclass, using its children as actual entities to define different inheritance mappings and associations (association mappings in mapped superclasses are very limited).
so in your specific case you could have such a structure:
/**
* I'm not an actual Entity!
*
* #MappedSuperClass */
Class ModuleSuperClass {}
/**
* I don't have children
*
* #ORM\Entity */
Class BaseModule extends ModuleSuperClass {}
/**
* I have children
*
* #ORM\Entity
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"page" = "Page"})
*/
Class AdminModule extends ModuleSuperClass {}
/**
* I'm just a child
*
* #ORM\Entity
*/
Class PageModule extends AdminModule {}
your mileage may of course vary, i.e. I would rather have a BaseModule class without children, and then a BaseModule in an entirely different namespace to extend both AdminModule and PageModule from.