I need a way to create relations on the fly, and I choose the Event of the doctrine that is launched when load the class meta data, loadClassMetadata()
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$metadata = $eventArgs->getClassMetadata();
$this->em = $eventArgs->getEntityManager();
if ($metadata->getName() != 'AppBundle\Entity\NewsNews') {
return;
}
$attachmentsMetadata = $this->getAttachmentsClassMetadata();
$attachmentsMetadata->mapManyToOne(
[
"targetEntity" => $metadata->getName(),
"fieldName" => "newsNews",
'joinColumns' => array(
array(
'name' => 'foreign_key',
'referencedColumnName' => 'id'
)
),
"inversedBy" => "attachments"
]
);
$attachmentsMetadata->initializeReflection();
$metadata->mapOneToMany(
[
"targetEntity" => $attachmentsMetadata->getName(),
"fieldName" => "attachments",
'joinColumns' => array(
array(
'name' => 'id',
'referencedColumnName' => 'foreign_key'
)
),
"mappedBy" => "newsNews"
]
);
}
Ok, worked but the problem is when the doctrine will set the data for this relations he throw this exception "Notice: Undefined index: newsNews"
I've checked the class when the doctrine will attach this data and the newsNews field is missing in the reflection properties.
I don't know if I forget some part of this process xD
Thanks for the help
Mappings are not meant to be changed at runtime, your proposed solution is more of a hack to Doctrine and, while it could work, it will probably lead you to more hacks along the way.
If your restriction is that you don't want to modify the Attachment mapping, you can do a one-to-many association with joined table.
This way, you only need to map the inverse side of the one-to-many. But, you'll only be able to navigate it that way.
Documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table
Related
How can I use has_one and has_many in codeigniter 3.0?
I would like to create relations through tables like in Ruby on Rails.
I.E
Class User
{
has_many: comments
}
Class Comments
{
belongs_to: users
}
This is the most common usage, and is used in almost every project. There is a simple pattern to defining this relationship.
Post has a creator and an editor, which may be different users. Here's how to set that up.
Post
class Post extends DataMapper {
$has_one = array(
'creator' => array(
'class' => 'user',
'other_field' => 'created_post'
),
'editor' => array(
'class' => 'user',
'other_field' => 'edited_post'
)
);
}
User
class User extends DataMapper {
$has_many = array(
'created_post' => array(
'class' => 'post',
'other_field' => 'creator'
),
'edited_post' => array(
'class' => 'post',
'other_field' => 'editor'
)
);
}
A couple of things to note here.
The relationship is now defined by the relationship key on either
side, not the model name. This has now become the only way to look
up the relationship.
The key on one side of the relationship becomes the other_field on
the opposite side, and vice-versa.
Because we need a way to specify the difference between posts that
were edited and those that were created, we have to declare the
slightly unusual edited_post and created_post relationships. These
could have any name, as long as they were unique and mirrored on
Post.
http://datamapper.wanwizard.eu/pages/advancedrelations.html
Just was wondering if ZF2's hydrating resultset can hydrate multiple entities. Consider the snippet below:
$sql = new Sql($this->adapter);
$sqlObject = $sql->select()
->from([
'ART' => 'acl_roles'
])
->join([
'ARTT' => 'acl_role_types',
],
'ART.type_id = ARTT.id',
[
'ARTT.id' => 'id',
'ARTT.identifier' => 'identifier',
'ARTT.name' => 'name',
'ARTT.status' => 'status',
'ARTT.dateAdded' => 'date_added',
],
Select::JOIN_INNER
)
->where([
'ART.identifier' => $identifier,
])
->columns([
'ART.id' => 'id',
'ART.type_id' => 'type_id',
'ART.identifier' => 'identifier',
'ART.name' => 'name',
'ART.status' => 'status',
'ART.description' => 'description',
'ART.dateAdded' => 'date_added',
]);
Now if the query was on a single entity, I could do something like:
$stmt = $sql->prepareStatementForSqlObject($sqlObject);
$resultset = $stmt->execute();
if ($resultset instanceof ResultInterface && $resultset->isQueryResult()) {
$hydratingResultSet = new HydratingResultSet(new ArraySerializable, new EntityClass);
$hydratingResultSet->initialize($resultset);
return $hydratingResultSet->current();
}
However in my case I need the hydrating result set to be able to build and return multiple entities (namely AclRoleEntity and AclRoleTypeEntity). Is this something that is possible? If yes how (considering the result set being a flat array of combination of both entities). If no are there better alternatives to achieve this without using Doctrine/Propel?
Thanks
It's totally possible, you're just going to need a configured (possibly custom) Hydrator.
Your hydrator will need to know the logic to inject your parameters into your objects from a flat array, and how to reduce your object models back to a flat array on extraction.
You're probably looking at a few Hydrator Strategies or a hydrator naming strategy and potentially a combination of both.
With the correct hydrator, you can achieve what you're looking for.
I am using Doctrine 2 in my Zend Framework 2 Project. I have now created a Form and create one of my Dropdowns with Values from the Database. My Problem now is that I want to change which values are used and not the one which I get back from my repository. Okay, here some Code for a better understanding:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'county',
'options' => array(
'object_manager' => $this->getObjectManager(),
'label' => 'County',
'target_class' => 'Advert\Entity\Geolocation',
'property' => 'county',
'is_method' => true,
'empty_option' => '--- select county ---',
'value_options'=> function($targetEntity) {
$values = array($targetEntity->getCounty() => $targetEntity->getCounty());
return $values;
},
'find_method' => array(
'name' => 'getCounties',
),
),
'allow_empty' => true,
'required' => false,
'attributes' => array(
'id' => 'county',
'multiple' => false,
)
)
);
I want to set the value for my Select to be the County Name and not the ID. I thought that I would need the 'value_options' which needs an array. I tried it like above, but get the
Error Message: Argument 1 passed to Zend\Form\Element\Select::setValueOptions() must be of the type array, object given
Is this possible at all?
I was going to suggest modifying your code, although after checking the ObjectSelect code i'm surprised that (as far as I can tell) this isn't actually possible without extending the class. This is because the value is always generated from the id.
I create all form elements using factories (without the ObjectSelect), especially complex ones that require varied lists.
Alternative solution
First create a new method in the Repository that returns the correct array. This will allow you to reuse that same method should you need it anywhere else (not just for forms!).
class FooRepository extends Repository
{
public function getCounties()
{
// normal method unchanged, returns a collection
// of counties
}
public function getCountiesAsArrayKeyedByCountyName()
{
$counties = array();
foreach($this->getCounties() as $county) {
$counties[$county->getName()] = $county->getName();
}
return $counties;
}
}
Next create a custom select factory that will set the value options for you.
namespace MyModule\Form\Element;
use Zend\Form\Element\Select;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
class CountiesByNameSelectFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $formElementManager)
{
$element = new Select;
$element->setValueOptions($this->loadValueOptions($formElementManager));
// set other select options etc
$element->setName('foo')
->setOptions(array('foo' => 'bar'));
return $element;
}
protected function loadValueOptions(ServiceLocatorInterface $formElementManager)
{
$serviceManager = $formElementManager->getServiceLocator();
$repository = $serviceManager->get('DoctrineObjectManager')->getRepository('Foo/Entity/Bar');
return $repository->getCountiesAsArrayKeyedByCountyName();
}
}
Register the new element with the service manager by adding a new entry in Module.php or module.config.php.
// Module.php
public function getFormElementConfig()
{
return array(
'factories' => array(
'MyModule\Form\Element\CountiesByNameSelect'
=> 'MyModule\Form\Element\CountiesByNameSelectFactory',
),
);
}
Lastly change the form and remove your current select element and add the new one (use the name that you registered with the service manager as the type key)
$this->add(array(
'name' => 'counties',
'type' => 'MyModule\Form\Element\CountiesByNameSelect',
));
It might seem like a lot more code (because it is) however you will benefit from it being a much clearer separation of concerns and you can now reuse the element on multiple forms and only need to configure it in one place.
I'm using php.activerecord, and I am trying to link tables together. I'm not using their structure, but php.activerecord assumes I am, so it doesn't always work. I'm trying to use it on an already made app, so I can't change the database.
I learned from my previous question - Model association with custom table and key names - that I need to be as explicit as possible with the primary_key and foreign_key fields.
I'm having issues now using has_many through. I keep getting NULL, and I have no idea why.
So, here's a scenario: I have 3 tables, contacts, contactPrefs, and preferences. Those tables are as follows
contacts
--------
contactID
name
status
contactPrefs
------------
contactID
prefID
prefValue
preferences
-----------
prefID
name
description
Each contact has multiple contactPrefs. Each contactPrefs has one preferences. I tried to use has_many to get this working, but it's not. Here are my models:
Contacts.php:
<?php
class Contact extends ActiveRecord\Model {
static $primary_key = 'contactID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'primary_key' => 'contactid',
'class_name' => 'ContactPref'
),
array(
'preferences',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'through' => 'prefs',
'class_name' => 'Preference'
)
);
}
ContactPref.php:
<?php
class ContactPref extends ActiveRecord\Model {
static $table_name = 'contactPrefs';
static $belongs_to = array(
array(
'contact',
'foreign_key' => 'contactid',
'primary_key' => 'contactid'
),
array(
'preference',
'foreign_key' => 'prefid',
'primary_key' => 'prefid'
)
);
}
Preference.php:
<?php
class Preference extends ActiveRecord\Model {
static $primary_key = 'prefID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'class_name' => 'ContactPref'
)
);
}
According to the docs, I now should be able to the following:
<?php
var_dump(Contact::find(1234)->preference);
I cannot. I get NULL. Oddly, I can do this:
<?php
var_dump(Contact::find(1234)->prefs[0]->preference);
That works correctly. But, shouldn't I be able to access the preference object directly through the contact object? Am I misunderstanding the docs (they aren't the greatest, in my opinion)? Am I doing something wrong?
First you are reading the docs with a small flaw. In the docs you are shown:
$order = Order::first();
# direct access to users
print_r($order->users); # will print an array of User object
Which you are already doing via Contact::find(1234)->prefs. Let me boil it down a bit
$contact = Contact::find(1234);
# direct access to prefs
print_r($contact->prefs); # will print an array of ContactPref object
Second, what you actually want is undefined. What should Contact::find(1234)->preference actually do? Return the preference of the first ContactPref? Return an array of Preference objects?
I feel like offering both:
<?php
class Contact extends ActiveRecord\Model {
static $primary_key = 'contactID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'primary_key' => 'contactid',
'class_name' => 'ContactPref'
),
array(
'preferences',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'through' => 'prefs',
'class_name' => 'Preference'
)
);
public function get_preference() {
return isset($this->prefs[0])
? $this->prefs[0]->preference
: null
;
}
public function get_preferences() {
$preference=array();
foreach($this->prefs as $pref) {
$preference[]=$pref;
}
return $preference;
}
}
Let me explain a little bit what I have done. The ActiveRecord\Model class has a __get($name) function that looks for another function called get_$name, where $name in your case is preference (for the first result) and preference (for the entire collection). This means you can do Contact::find(1234)->preference which would be the same as doing Contact::find(1234)->prefs[0]->preference (but safer, due to the check) and Contact::find(1234)->preferences to get the entire collection of preferences.
This can be made better or optimized in numerous ways, so please don't take it as it is, but do try and adapt it to your specific situation.
For example you can either use the id of the preference as an index in the array or either not force a load of more data from ContactPrefs than the ones you are going to use and try a more intricate query to get the preference objects that you specifically need.
If I find a better implementation by getting through to work in the relationship definition, I'll return. But seeing the Unit Tests for active record, I'm skeptical.
There are several things that look strange, so it's not easy to come to a "this will fix it" for you, but this is an issue at least:
Fieldnames should always be lower-case in phpactiverecord. SQL doesn't mind it either way (not that table names ARE case-sensitive, but column names aren't). So make this:
static $primary_key = 'contactID';
into
static $primary_key = 'contactid';
The connections // find commands can be used in SQL, in which case it doesn't really matter how your key-string is 'cased', so some stuff works. But if the connection goes trough the inner-workings of phpmyadmin, it will fail. So check out this contactID but also the prefID.
Again, this goes only for COLUMN names, so don't go changing classnames or table-names to lowercase.
(extra point: phpmyadmin has trouble with combined primary keys. So while it might be ugly, you could add an extra row to your contactprefs table (if you don't allready have it) called id, to make that table actually have something to work with. It wouldn't give you much trouble, and it would help the activerecord library a lot)
Try the following:
<?php
var_dump(Contact::find(1234)->preferences);
The documentation says that with a has_many relationship, it should be referenced by a plural (http://www.phpactiverecord.org/projects/main/wiki/Associations#has_many_through). The Contact::find(1234) returns a Contact object which has multiple contactPrefs with their each Preference. In addition, in your Contact model, you specify the has_many as preferences .
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'primary_key' => 'contactid',
'class_name' => 'ContactPref'
),
array(
'preferences',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'through' => 'prefs',
'class_name' => 'Preference'
)
);
Edit Through Modification:
Try the following Contact model
<?php
class Contact extends ActiveRecord\Model {
static $primary_key = 'contactID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'class_name' => 'ContactPref'
),
array('preferences',
'through' => 'prefs',
'class_name' => 'Preference',
'primary_key' => 'prefID')
);
}
I am working on a private messaging system using PHP-ActiveRecord ORM. I have models called 'User' and 'Message', MySQL tables called 'users' and 'messages', and in the messages table, I have fields called 'sender_id' and 'recipient_id'. However, I have no idea how to properly associate senders and recipients to the User model.
This is what I have so far in the User model:
static $has_many = array(
array('messages'),
array('recipients', 'foreign_key' => 'recipient_id', 'class_name' => 'Message'),
array('senders', 'foreign_key' => 'sender_id', 'class_name' => 'Message'),
);
and this is what I have so far in the Message model:
static $belongs_to = array(
array('sender', 'class_name' => 'User'),
array('recipient', 'class_name' => 'User'),
);
However, when I run the code such as $message->recipient->first_name, it does not properly pull the first name from the User model, like I want it to. I am not sure what I am doing wrong.
Normally, I just use the standard naming conventions. However, for this example, we have to have sender_id and recipient_id, which are both the same type, so I can't just use the standard naming conventions as I have been doing.
Any help would be highly appreciated. I am using the PHP-ActiveRecord ORM. I think the Rails version is more widely used, but it is my understanding that they work the same except with different syntax.
You also need to specify the foreign keys in the $belong_to, otherwise they will be inferred from the table's name (i.e. user_id):
static $belongs_to = array(
array('sender', 'class_name' => 'User', 'foreign_key' => 'sender_id'),
array('recipient', 'class_name' => 'User', 'foreign_key' => 'recipient_id'),
);