in config.yml
edit:
title: 'editing %firstname% '
fields:
- {property: firstname, type: string}
- {property: lastname, type: string}
- {property: email, type: string}
- {property: password,type: password}
- {property: mobilenumber, type: string}
- enabled
i want the username to come up dynamically and %firstname% doesn't work.
You can only display entity ID in edit view like this:
title: 'editing %%entity_id%%'
Starting from there, you can use Doctrine composite primary key to include firstname (or username?) in your primary key on User class:
/** #ORM\Id #ORM\Column(type="string") */
private $username;
Related
As the title states, I am trying to add a custom property to the serialized object I return.
Let's take a User with the following methods:
getFirstname, setFirstname
getLastname, setLastname
getUsername, setUsername
...
Now in the serialization I would like to add a property fullName: Firstname + Lastname.
I have a getter method in my entity like so:
/**
* get name
*
* #return string
*/
public function getName()
{
return $this->getFirstname()." ".$this->getLastname();
}
My serialization file looks something like this:
AppBundle\Entity\User:
exclusion_policy: ALL
properties:
id:
expose: false
username:
expose: true
groups: [list, details]
email:
expose: true
groups: [details]
name:
expose: true
groups: [list, details]
I have tried with
name:
expose: true
groups: [list, details]
access_type: public_method
type: string
serialized_name: fullName
accessor:
getter: getName
and other variants but I can't seem to get it right.
Note: Yes I've cleared my cache and tried it again.
Anyone able to tell me what I am missing ?
Thanks in advance !
Since your full name is not a property at all you have to define a virtual property:
AppBundle\Entity\User:
exclusion_policy: ALL
properties:
# All properties but not name
virtual_properties:
getName:
groups: [list, details]
serialized_name: fullName
I am working with a bundle called rss-atom-bundle. Through this bundle I should be able to fetch the RSS feeds and save them to the database.
I can get the bundle to work up till getting the feeds from the URL but when I try to presist I get the following error and I just cant figure it out why I am getting it.
The class 'Debril\RssAtomBundle\Protocol\Parser\Item' was not found in the chain configured namespaces Symfony\Bundle\AsseticBundle\Entity, AppBundle\Entity`
Looking online and at stackoverflow i came across questions that were related to this but all of them are talking about by default the Entity should be inside the Entity folder of the Bundle and if they are not then this error appears but my Entity files are inside Entity folder and I followed the instructions given by the bundle author but still i cant get passed this error
This is what my Controller looks like
$em = $this->getDoctrine()->getManager();
// fetch the FeedReader
$reader = $this->container->get('debril.reader');
// this date is used to fetch only the latest items
$unmodifiedSince = '01/01/2000';
$date = new \DateTime($unmodifiedSince);
// the feed you want to read
$url = 'http://example.com/feed/';
$feeds = $reader->getFeedContent($url, $date);
$items = $feeds->getItems();
dump($items);
foreach ( $items as $item ) {
$em->persist($item);
}
$em->flush();
As per bundle instructions I did implement FeedInterface to my Feed Entity and ItemInInterface, ItemOutInterface to my Item Entity
This is what my orm.yml looks like for Feed and Item Entity
Feed
AppBundle\Entity\Feed:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
description:
type: text
link:
type: text
publicId:
type: text
lastModified:
type: datetime
lastViewed:
type: datetime
oneToMany:
items:
targetEntity: AppBundle\Entity\Item
mappedBy: feed
cascade: ["persist"]
lifecycleCallbacks: { }
Item
AppBundle\Entity\Item:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
title:
type: text
nullable: true
summary:
type: text
nullable: true
description:
type: text
nullable: true
medias:
type: text
nullable: true
updated:
type: string
nullable: true
publicId:
type: string
nullable: true
link:
type: string
nullable: true
comment:
type: string
nullable: true
author:
type: string
nullable: true
manyToOne:
feed:
targetEntity: AppBundle\Entity\Feed
inversedBy: items
joinColumn:
name: feed_id
referencedColumnName: id
Any help will be really appreciated as I am clueless why i am getting the error?
Try to add RssAtomBundle to the list with mappings in app/config/config.yml
orm:
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
AsseticBundle: ~
RssAtomBundle: ~
My title might not be the best description, but it's best I could come up with.
I have 4 entities; Page, PageElement, Image and an entity called TextWithImage. Page holds pageElements (array of PageElement entities). Those pageElements can be of numerous types, but for now I have only one called TextWithImage that's holding additional data to the data the PageElement entity holds.
The PageElement can be included on numerous pages and so, I have a ManyToMany in the PageElement.orm.yml. The TextWithImage has a manyToOne to reference to Image.
(More information: Another Entity ImageGallery might have a manyToMany relationship with the Image entity, while TextOnly shouldn't have any reference to the Image entity.)
I want to be able to get the Page and retrieve the PageElements with all their "attributes". So let's say I request to get a Page with only one TextWithImage type of PageElement, I want to return the following.
Page -> pageElements = array (
[0] => TextWithImage -> image = Image -> filename = "image.png"
-> alt = "An image!"
-> text = "There's an image too!"
)
All seems simple enough, but I need doctrine to understand that this PageElement is a TextWithImage type. Can I do this with a DiscriminatorColumn, say (rough sketch);
Table: pageelement
id | attributes | discr | TextWithImageId
Table: textwithimage
id | attributes
Keep in mind that I'll have more than just one type of PageElement, not only TextWithImage.
Is this possible and if so, how?
I've found the solution to my problem. These are the doctrine YML files. You can generate all entities with php app/console doctrine:generate:entities AppBundle/Entity. Make sure that the PageTextImageElement class extends the PageElement class.
Page.orm.yml
AppBundle\Entity\Page:
type: entity
table: null
repositoryClass: AppBundle\Repositories\PageRepository
manyToMany:
pageElements:
targetEntity: PageElement
cascade: ["all"]
joinTable:
name: null
joinColumns:
page_id:
referencedColumnName: id
onDelete: CASCADE
inverseJoinColumns:
page_element_id:
referencedColumnName: id
unique: true
onDelete: CASCADE
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: '255'
unique: true
lifecycleCallbacks: { }
PageElement.orm.yml
AppBundle\Entity\PageElement:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
discriminatorMap:
pageTextImageElement: PageTextImageElement
table: null
repositoryClass: AppBundle\Repositories\PageElementRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
sortOrder:
type: integer
attributes:
type: array
nullable: true
lifecycleCallbacks: { }
PageTextImageElement.orm.yml
AppBundle\Entity\PageTextImageElement:
type: entity
table: null
oneToOne:
image:
targetEntity: AppBundle\Entity\Image
joinColumn:
name: imageId
referencedColumnName: id
fields:
passage:
type: string
length: '255'
lifecycleCallbacks: { }
Image.orm.yml
AppBundle\Entity\Image:
type: entity
table: null
repositoryClass: AppBundle\Repositories\ImageRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: '255'
unique: true
description:
type: string
length: '255'
lifecycleCallbacks: { }
When running:
php app/console doctrine:schema:validate
I receive the error:
[Mapping] FAIL - The entity-class 'Path\ToBundle\Entity\Variant' mapping is invalid:
* The referenced column name 'localsku' has to be a primary key column on the target entity class 'Path\ToBundle\Entity\Inventory'.
The killer here is that 'localsku' is indeed a primary key. Am I missing something major here?
Thanks in advance for any assistance, and I apologize if I've missed some key piece of information.
Variant Entity is defined as:
Path\ToBundle\Entity\Variant:
type: entity
table: variant
uniqueConstraints:
sku:
columns:
- sku
id:
variantId:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
column: variant_id
generator:
strategy: IDENTITY
fields:
name:
type: string
nullable: false
length: 255
fixed: false
comment: ''
sku:
type: string
nullable: false
length: 255
fixed: false
comment: ''
oneToOne:
inventory:
targetEntity: Inventory
cascade: { }
mappedBy: null
inversedBy: variant
joinColumns:
sku:
referencedColumnName: localsku
orphanRemoval: false
lifecycleCallbacks: { }
Inventory Entity is defined as:
Path\ToBundle\Entity\Inventory:
type: entity
table: Inventory
id:
localsku:
type: string
nullable: false
length: 255
fixed: false
comment: ''
id: true
column: LocalSKU
generator:
strategy: IDENTITY
fields:
itemname:
type: string
nullable: true
length: 255
fixed: false
comment: ''
column: ItemName
qoh:
type: integer
nullable: true
unsigned: false
comment: ''
column: QOH
location:
type: string
nullable: true
length: 250
fixed: false
comment: ''
column: Location
barcode:
type: string
nullable: true
length: 25
fixed: true
comment: ''
column: Barcode
oneToOne:
variant:
targetEntity: Variant
cascade: { }
mappedBy: inventory
inversedBy: null
joinColumns:
localsku:
referencedColumnName: sku
orphanRemoval: false
lifecycleCallbacks: { }
Variant Entity reference to Inventory:
/**
* Inventory
*
* #var \Path\ToBundle\Entity\Inventory
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Inventory", inversedBy="variant")
* #ORM\JoinColumn(name="sku", referencedColumnName="localsku")
*/
protected $inventory;
Inventory Entity reference to Variant:
/**
* Variant
*
* #var \Path\ToBundle\Entity\Variant
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Variant", inversedBy="inventory")
* #ORM\JoinColumn(name="localsku", referencedColumnName="sku")
*/
protected $variant;
Your association mapping is wrong.
It should be something like that, assuming Variant table have a "sku" field.
Variant Entity :
/**
* Inventory
*
* #var \Path\ToBundle\Entity\Inventory
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Inventory", inversedBy="variant")
* #ORM\JoinColumn(name="sku", referencedColumnName="localsku")
*/
protected $inventory;
Inventory Entity :
/**
* Variant
*
* #var \Path\ToBundle\Entity\Variant
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Variant", mappedBy="inventory")
*/
protected $variant;
http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-one-bidirectional
P.S : I'm curious. You are using both yml configuration and annotation for the same entities ? I'm not sure that doctrine is able to merge them, does it works ?
I'm trying to realize a many-to-many relationship between two tables.
this is my configuration:
Mailer\EmpfaengerBundle\Entity\Empfaenger:
type: entity
table: empfaenger
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
vorname:
type: string
length: 255
nullable: true
nachname:
type: string
length: 255
nullable: true
created_by:
type: integer
updated_by:
type: integer
manyToMany:
verteiler:
targetEntity: Verteiler
mappedBy: empfaenger
Mailer\EmpfaengerBundle\Entity\Verteiler:
type: entity
table: verteiler
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
nullable: true
created_by:
type: integer
updated_by:
type: integer
manyToMany:
empfaenger:
targetEntity: Empfaenger
inversedBy: verteiler
joinTable:
name: verteiler_sys
joinColumns:
verteiler_id:
referencedColumnName: id
inverseJoinColumns:
empfaenger_id:
referencedColumnName: id
Now when I try to edit an entry, it works in the "verteiler" table, but not in the empfaenger table.
The form is shown in the "empfaenger" edit page and the entries are highlightet, but when i change ohne, it won't save the changes.
I tried various different configurations according to the doctrine documentation but always get the same result :-(
both entities have exactly the same structure:
/**
* Add empfaenger
*
* #param \Mailer\EmpfaengerBundle\Entity\Empfaenger $empfaenger
* #return Verteiler
*/
public function addEmpfaenger(\Mailer\EmpfaengerBundle\Entity\Empfaenger $empfaenger)
{
$this->empfaenger[] = $empfaenger;
return $this;
}
/**
* Remove empfaenger
*
* #param \Mailer\EmpfaengerBundle\Entity\Empfaenger $empfaenger
*/
public function removeEmpfaenger(\Mailer\EmpfaengerBundle\Entity\Empfaenger $empfaenger)
{
$this->empfaenger->removeElement($empfaenger);
}
/**
* Get empfaenger
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getEmpfaenger()
{
return $this->empfaenger;
}
and help would be appreciated.
all right, i found a solution, in case anybody faces this:
http://www.youtube.com/watch?v=kPrgoe3Jrjw