Let's say we have a 1:n relation between an Author and Book model. Modeling the relation using the Extension Builder, the necessary code to get Book's for an author is provided.
But what about the inverse? How do I get the Author for a given Book class?
I assume you have created the following domain model with the Extension Builder.
When you create a 1:n relation between Author and Book, the resulting database table for Book will contain a field which holds the UID of the author. To make use of this field, you have to add a getter in your book domain model to return the corresponding author for the given book.
Add the following to your domain model for book:
/**
* Returns the author
*
* #var \TYPO3\YourExtension\Domain\Model\Author
*/
protected $author;
/**
* #return \TYPO3\YourExtension\Domain\Model\Author
*/
public function getAuthor() {
return $this->author;
}
Now you can use the new getter in Fluid to return the author of a given book with {book.author}
You can add n:1 relation to Book model, then Extension builder will create getter and setter automatically..
Related
We defined in TYPO3 9.5 this field in a model:
/**
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\GeorgRinger\News\Domain\Model\News>
* #TYPO3\CMS\Extbase\Annotation\ORM\Lazy
*/
protected $txIctivigoNoticesFrom;
/**
* Get notices from
*
* #return \GeorgRinger\News\Domain\Model\News[]
*/
public function getTxIctivigoNoticesFrom()
{
return $this->txIctivigoNoticesFrom;
}
/**
* Set notices from
*
* #param \GeorgRinger\News\Domain\Model\News[] $txIctivigoNoticesFrom
*/
public function setTxIctivigoNoticesFrom($txIctivigoNoticesFrom)
{
$this->txIctivigoNoticesFrom = $txIctivigoNoticesFrom;
}
But now we want to allow this is an array of not only news records, but pages and sys_file_metadata records as well. How could we achieve that?
We tried to instantiate an abstract entity between <>, but it's not possible.
TCA field is defined properly as a group type, and it saves relations to these three tables.
There is no ready-to-use functionality in extbase to resolve such mixed references into objects. In the database field, the content is a comma separated list of identifiers, which are of the form TABLENAME_UID, like tt_content_1670,tt_content_1672,pages_123. So your only choice would be to receive those values as string(s) and then load the objects yourself.
With this solution, keep in mind, that there are no default extbase models for the core elements like tt_content, page and so on. So you will either have to use something like https://github.com/georgringer/news/blob/master/Classes/Domain/Model/TtContent.php and https://github.com/georgringer/news/blob/master/Classes/Domain/Repository/TtContentRepository.php and/or write the corresponding models and repositories yourself!
I you don't need extbase objects, but just the plain database data for some reason, you can also load this directly from the database.
I have a 1:n relation between two model objects: MainAuthor and Book. I created my domain model with the Extension Builder and I need to get the MainAuthor in the Book class, just like this: TYPO3 Extbase bidirectional 1:n relation
I followed the instructions, but book->getMainAuthor() returns null. Is there something else I should do?
Did you name your property correctly in Book (should be $mainAuthor)?
Did you empty your caches?
I am using Symfony 2.5 and one of my entity has to save multiple languages saved against the entity while create/update.
I saw a list of languages available in Symfony core[Resources] and a field type language also presents to generate a language field for form.
So, How do I map one-to-many relationship with the core list of languages from my Entity in Doctrine? I can't, because I don't have any existing entity to serve a list of languages to be added to target-entity in ORM mapping.
Or Do I have to create a separate custom language entity for the same?
If you dont want to create annoter entity for that you can use "array" type to stock languages ids
/**
* #Column(type="array", nullable=true)
* #var array
*/
private $languages;
array: Type that maps a SQL CLOB to a PHP object using serialize()
and unserialize() - See more at:
http://doctrine-orm.readthedocs.org/en/latest/reference/basic-mapping.html
In Grails if there is a many to many relation ships for example book has many authors
Lets says book1 has author1,autho2,author3,author4 values .Now a PUT request is sent saying that book1 has only author1,author3 then the other two values are deleted from the table.
Now for the same scenario with one to many relation ship lets say book1 has author1,autho2,author3,author4 values now if a PUT request is done with only author1,author3
Is it suppose to delete the other two i.e, author2 and author4 values?? i expect the behavior to be so..
Below is my model for book and author
class Author {
String name;
static hasMany = [books: Book]
static mapping = {
books cascade: 'all-delete-orphan'
}
}
class Book{
String name
static belongsTo = [author: Author]
}
EDIT:
When i emplement the all-delete-orphan i get the followinh error
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance
It depends on the cascade options set. If you are using belongsTo on the Author table grails uses cascade all by default. This means that the Author objects are removed if they are not used by any other Book. You can customize this behaviour using the cascade option (all-delete-orphan should be the value you are interested in).
I need little help :). Here is the situation. I am using symfony2 + FOSUserBundle, I made my forms custom, so far so good. I have User registration with user information in the custom registration form (like first name, last name, birth date etc). Now I decided that it will be more practical to make the user info to be stored in mongodb as document (as I probably will add more information to users later). I built the user info form, and successfully embedded it to the user form. Now the problem is that I cannot set Document object inside Entity object - symfony tells me that the object must be an Entity.
/**
* Acme\UserBundle\Entity\User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User extends BaseUser
{
/**
* #Assert\Type(type="Acme\UserBundle\Document\UserInfo")
*/
protected $userinfo;
I want to ask, what is the proper way to do this ? Sure I can get the needed information form the request as an array and fill in the user info object ... but it looks ugly and wrong :) so how it must be done ? Thanks.
I assume you extends the entity class
FOS\UserBundle\Entity\User
there is a document class provided by the bundle
FOS\UserBundle\Document\User
You could extends this one