I have a polymorphic association (Class Table Inheritance) and I need use DQL to query entities of a specific child class wich can be done using "x INSTANCE OF Entity" in WHERE clause. Now I need to put conditions specific for that child class but I get this error:
"Class Person has no association named student_field_1"
Person = Parent Class
Employee = Child class
Student = Child class
is there any way yo cast of somehow tell Doctrine that the Person is actually a Student and to allow me to put Student fields in the WHERE?
It sounds like a "Mapped Superclass" would be more suited to what your trying to do as it doesn't require an explicit link between parent / child, its just simple inheritance.
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html#mapped-superclasses
With Class Table Inheritance your required to provide discriminator map that links the two entities through a key.
"The table of a child class should be linked to the table of a parent class through a foreign key constraint"
If you're only querying students, then why don't you just do this?
SELECT s FROM Student s WHERE s.student_field_1 = ...
Related
I'm using the most recent versions of ZF2 and doctrine2 orm in a project.
What I want to achieve, is a generic class "Comment" that has a relation to another entity. This entity could be from various classes "Post", "Image" aso., depending on what the comment should be related to.
The idea was to have one table for all comments and a column that defines the type of related entity, just like using the discriminator mapping.
Problem with the discriminator is that I have to create different classes using extends, for every use case. This does not seem efficient as I will have to create quite a lot of subclasses to reflect all relations needed.
I would like to use just the base class of Comment and have one or two column(s) defining the relation like "Post:25" (target class and id in one column) or "Post" | "25" (target class an id in separate columns).
Is this possible in doctrine or is there a besser way of dealing with this?
Groundwork
I have a database design (snipped to relevant portions) like so:
records
entities
people
where no proper relationship exists between the tables, but rather records is extended by entities is extended by people by sharing the same id.
I have my Eloquent models similarly defined so that Record is extended by Entity is extended by Person.
Goal
Record and Entity are abstract and I want to simply use Person and other models at that level in the hierarchy.
Problem
How do I 'link' Record and Entity to Person to make this all work seamlessly? I don't think relationships is appropriate. I assume it's related to something like this:
protected __construct() {
parent::__construct();
}
I suggest you still create a model for Person, Entity, and Record, and link them together with "belongsTo" relationships. (add entity_id column to "People" table, add "record_id" to "Entities" table).
If you are only working with the Person model, you could add a "saved" model observer to create the entity and record entries whenever you create a Person, and tie them all together with a global id.
http://laravel.com/docs/eloquent#model-observers
If you did it this way, you could access the person's entity and record field by doing something like:
$person->entity->fieldName
$person->record->fieldName
All that being said, this seems way too complicated. I would only have one table for People and store all the person's data there. imo.
Recently started working with OOP in PHP. Following the "code to an Interface" principle, i got confused as to the type hint to use when passing a single object or multiple as argument to a method.
Currently, i have a "Student" class - represents a row in my students table, i also have a "Students" class that holds multiple student objects in an array.
To fetch the profile of one student, i pass the Students object (holding a single student object) to the profile class. I set a Students type hint in the profile class.
Now i feel this is bad code as i have lines like this
student = new Students();
and students = new Students();
question is,
am i on the right path?
if i remove the Students class and work with Student alone, based on the principle, how do i pass multiple Student objects (assuming array) to the profile class if it accepts a Student type hint?
what options do i have?
Thanks.
If by Students you mean a collection of Student objects, perhaps a better name would be StudentCollection or StudentSet.
There are two ways around the type hint problem:
Introduce a method on StudentCollection called ->getProfiles(); it would return an array of profiles for each Student instance it's managing by calling methods on Profile.
Introduce a (static) method on Profile that operates on a StudentCollection instance.
The first option has feature envy, which is why I've included a workaround.
Instead of reinventing the wheel you might want to try Doctrine or at least take a look at its architecture.
I'm not sure if I get your exact issue... But if you want to go for your own code I would first abstract the DB layer as well and have some base classes like Database, Table, Row, Field that an describe the DB stack and extend them as needed with some magic methods. So when you do Student extends Table it would automatically check for a "students" table or whatever else convention you like to implement. Alternatively you could just pass the table name as arg.
Whatever Object is returning the result set from the database would have to construct a single Row object for each row and add it to a collection of rows that I would name ResultSet and contains all the row objects and return that collection.
I'm writing constructors for my classes in a Doctrine2 application, let's say Fruits, Apple, Bananas.
Fruits is the parent class, where Apples and Bananas inherit from Fruits using single table inheritance on field type.
On the Doctrine2 documentation page, there is an example provided for single table inheritance. If we are always discriminating using Single Table Inheritance, should the base class Fruits be abstract because the discriminator field must always be set? If so, should the constructor for Fruits also be protected to prevent this behavior?
As there are no methods in your parent class "Fruits" that you need to redeclare I don't think there is an explicit need for it to be declared as abstract.
Also you may find a use case where you may want an instance of "Fruit" to be persisted (undetermined as to what type of fruit it is). Marking the parent as abstract will prevent you from being able to do this.
Maybe fruit is a bad example. But the Person example they have in the documentation is better. Employees will inherit Person definitions. But I may also want to persist just an instance of Person, undetermined of type. Hence the "person" = "Person" in the #DiscriminatorMap.
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html#single-table-inheritance
I have an object model that contains a class with several subclasses.
The subclasses share a few fields from the parent, but they each have
their own fields as well.
I'm using the column_aggregation inheritance type to do this because I
want to be able to polymorphically store objects of the parent class
type, but retrieve objects of the subclass types.
So far, I can retrieve the objects with their subclass types intact.
My problem is that when I call getColumns() on any of the subclass , I
get all the fields for the aggregated table, so I see every field from
all the subclasses together.
Is there a way to only get the fields that actually belong the
subclass?
Design-wise the output of getColumns() is correct (it merely returns all columns of the aggregated table). I think Doctrine_Table->getColumns() is oblivious to the fact that the table in question has sub-classes. Try to use Doctrine_Table->getColumnDefinition() or as a last resort Doctrine_Table->getColumnOwner($column) to infer which columns belong to which sub class.