CakePHP 2.6.x
I used the bake CLI to create my models, which created fields named ID. Notice it's uppercase.
So in my models, I was expecting to reference that property like this: $this->ID, as the property names usually match the field names (in my experience). It's definitely working that way in the controllers. For example, I have lots of controller code that looks like this:
$this->SomeModel->findById( $model['SomeModel']['ID'] );
However this didn't work in the model. After a lot of head scratching and experimenting, I finally figured out that the model property is named id (notice the lower case).
//in SomeModel.php
var_dump( $this->ID ); //NULL
var_dump( $this->id ); 33
Is this the expected behavior? Do all model properties get converted to lower case? If so, why is the controller different? Did I defy a CakePHP convention somehow? Any explanation of what is going on here would be most welcome.
When you call $this->id you're accessing the Model's id property, not the value of the field in the database.
From source;
<?php
/**
* Value of the primary key ID of the record that this model is currently pointing to.
* Automatically set after database insertions.
*
* #var mixed
*/
public $id = false;
As Mark's suggested in his comment, use $this->primaryKey = 'ID' in your model to achieve the desired result, you could then do something like this in 2.6:
<?php
$this->id = 33; // Set the active record
$this->field('ID'); // Returns 33 (If you really want to use uppercase)
$this->id; // Returns 33
$this->read(); // Returns all of record 33
Related
I am creating a custom ACL class that would check whether the relationship exists between the records and if so load all the related records to that particular bean. I have looked at the sugar documentation which says to use load_relationship($relationshipName) for checking if relationship exists and getBeans() to load all the related records (as an array of objects). I have implemented this into my class but for some reason whichever module and relationship I use it always returns an empty array.
The data I use for checking has 3 parts:
The Module accessing the data
The relationship name with the target module (not module name)
The ID of the record accessing the data
The link here at sugar community shows a similar problem that I'm having, but the answer to this does not so solve my problem
Here is my custom ACL:
namespace Sugarcrm\Sugarcrm\custom\clients\base;
class CustomACL
{
const ACL_NONE = 0;
const ACL_READ_ONLY = 1;
const ACL_READ_WRITE = 2;
public static function checkRelated($module, $linkedRelationshipName, $id)
{
$bean = \BeanFactory::getBean($module);
if ($bean->load_relationship($linkedRelationshipName)) {
return self::checkRecordRelated($bean, $id,$linkedRelationshipName);
} else {
return false;
}
}
/**
* Checks if record is related
* #param $bean
* #param $id
* #param $linkedModule
* #return bool
*/
protected static function checkRecordRelated($bean, $id, $linkedModule)
{
$bean->retrieve_by_string_fields(array(
"id" => $id
));
if ($bean->load_relationship($linkedModule)) {
$relatedRecords = $bean->$linkedModule->getBeans();
return $relatedRecords;
} else {
return false;
}
}
}
This class should be working for any module, even if it is custom or non custom. I have tried using my custom module and even the default modules (leads, accounts etc) but none of them returns anything except an empty array.
I suspect the problem is that you are reusing the previously empty bean, for which you already loaded the same link using load_relationship() before.On the second load_relationship() call, Sugar probably returns the cached result from the first call (as the link is already internally flagged as having been loaded), therefore returning the same empty array again.
Therefore instead of using
$bean->retrieve_by_string_fields(array(
"id" => $id
));
I'd suggest creating a new bean e.g. using
if (empty($id)) {
return false;
}
$bean = BeanFactory::retrieveBean($module, $id);
if (!$bean) {
return false;
}
(which should actually not be too slow, as the bean is probably cached already)
Notes:
Your variable names are somewhat confusing. $linkedRelationshipName and $linkedModule should contain neither the relationship name nor the module name, but the name of the link-type field.
EDIT:
To reiterate:
The documentation may be misleading there, but load_relationship() does not expect the relationship name as parameter. What it expects is the link name!.
from data/SugarBean.php:
/**
* Loads the request relationship. This method should be called before performing any operations on the related data.
*
* This method searches the vardef array for the requested attribute's definition. If the attribute is of the type
* link then it creates a similary named variable and loads the relationship definition.
*
* #param string $link_name link/attribute name.
*
*#return nothing.
*/
function load_relationship($link_name)
So make sure to check the VarDefs of each module for the correct link name.
E.g.
relationship name: accounts_contacts
link field for this relationship in account: contacts,so you should be calling $accountBean->load_relationship('contacts')
link field for this relationship in contact: accounts,so you should be calling $contactBean->load_relationship('accounts')
Note: link names are basically arbitrary across different modules, don't rely on them being lowercase singular/plural of the linked module. In some cases (and for custom relationships) they will not be.
I have a member of my entity is an arrayCollection. With a classic form builder is working fine, I can select multiple items and persist it. But when I try to update an object in controller I get the error : "Call to a member function setFaavailability() on array".
A resume of my entity :
/**
* #ORM\ManyToOne(targetEntity="App\Entity\FaAvailability",
inversedBy="faavailability")
* #ORM\JoinColumn(nullable=true)
* #ORM\Column(type="array")
*/
public $faavailability;
/**
* #return mixed
*/
public function getFaavailability()
{
return $this->faavailability;
}
/**
* #param mixed $faavailability
*/
public function setFaavailability($faavailability)
{
$this->faavailability = $faavailability;
}
In my controler :
$varFaavailability = $animal->faperson->getFaavailability();
foreach($varFaavailability as $availability){
if($availability->getName() == $animal->typepet->getName()){
$varFaavailability->removeElement($availability);
$faPerson = $em->getRepository(FaPerson::class) >findById($animal->faperson->getId());
$faPerson->setFaavailability($varFaavailability);
$em->persist($faPerson);
$em->flush();
}
}
Any ideas ?
If I remember well, when you set a field as an ArrayCollection it means that you have a oneToMany relationship between two entities.
From your code, I can tell you that you are trying to persist the data in the wrong entity. You usually add the owning_entity_id(1-to-N) in each item(1-to-N) and persist it. In your code, you are trying to set all the references at once, which is never going to happen. Delete the setFaavailability() or redefine the entities' relationships.
You should never try to mass-add foreign key relationships in one super duper setter function. Cycle through all the items and set the reference to the "parent" entity.
The problem is in this part: $faPerson = $em->getRepository(FaPerson::class)->findById($animal->faperson->getId());
The findBy* methods will try to find multiple entities and return them in a Collection.
If you're looking for a single person, you can use findOneById instead. Or (assuming id is configured as identifier in Doctrine) you can even use the find method: $faPerson = $em->getRepository(FaPerson::class)->find($animal->faperson->getId());
some general comments:
In Doctrine you never have to work with the IDs. Use the entity
objects! You only need to findById if you get the ID from a request parameter for example.
You should reconsider the naming of your variables to make it clear if it is a collection ($availabilities) or a single one ($availability).
Always use the getter/setter methods instead of the fields (typepet vs getTypepet()).
Call flush() one at the end to update all entities in one single transaction.
I've renamned the variables below as I understood them. However I am still not sure what $animal->faperson->getFaavailabilities() returns, since at the beginning you wanto to loop through the results and later set it to a single one via setFaavailability()?
//Should be a Doctrine ArrayCollection
$varFaavailabilities = $animal->faperson->getFaavailabilities();
foreach($varFaavailability as $availability){
if($availability->getName() == $animal->getTypepet()->getName()) {
//Why do you want to remove an element from the current loop?
$varFaavailability->removeElement($availability);
//No need to use Id
$faPerson = $animal->getFaperson();
//A single one?
$faPerson->setFaavailability($availability);
//More than one? addFaavailability should exist.
$faPerson->addFaavailability($availability);
$em->persist($faPerson);
}
}
$em->flush();
So I came across a weird issue while writing tests in laravel using factories. So this is a test I wrote:
/**
#test
*/
public function document_belongs_to_a_patent()
{
$patent = factory(Patent::class)->create();
$document = factory(Document::class)->create([
'documentable_id' => $patent->id,
'documentable_type' => 'patent'
]);
$this->assertArraySubset($patent->toArray(), $document->documentable->toArray());
}
So this should work, right because both should return the same thing and patent array should be equal or a subset of documentable array. But it was failing when I realised that there is an enum field in Patent model to which I am passing the value 1 but it was converted to the enum equivalent value in the database and when I tried document->documentable->toArray() it came back with the enum value rather than 1 which got me thinking how can make the model factory return the actual enum value and not the index number.
Top of the head I just fetched the patent just after creating it via the factory like so:
$patent = Patent::find($patent->id);
And it works well but it seems inconsistent. Is there a way to refresh models. I know we can refresh relationships of models but is there a way to do for the models themselves?
If you're strictly needing the change for API, you can do something cheeky with mutators like this.
https://laravel.com/docs/5.1/eloquent-serialization
Add this property. It tells Laravel that for special outputs only, it needs to append a non-database property.
protected $appends = ['documentable_type_name'];
Then you need some ways of knowing the language for the enum. You need an array, a #lang definition, etc. Here's a protected property solution that I am quite fond of in simple situations.
protected static $documentable_types = [ 'divorce', 'patent' ];
And then create this mutator on your Documentable model.
public function getDocumentableTypeName()
{
if ($this->documentable_type)
{
return static::$documentable_types[ $this->documentable_type ];
}
return null;
}
This changes your JSON output to look like this:
{
docuemntable_id : 555,
documentable_type : 1,
documentable_type_name : 'patent'
}
You can also hide the document_type field by adding this.
protected $hidden = ['documentable_type'];
And Laravel magic takes care of the rest. Hope that helps.
I am currently learning Symfony and Doctrine by reading the docs.
I don't understand the difference between find and findOneById. I tried to use them both in this simple example and it looks they do the same thing to me.
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:ProductEntity')
->findOneById($id);
Are they really the same thing or there is some difference? And where I can find the detailed documentation for all these methods?
In your case, they happen to do the same thing. Looking at this example, you'll notice that find() looks for the field named after the primary key. findOneBy<Field>() will explicitly use the field in the name of the method, even if it's not the primary key, and will return the first record. So, in the end, if the primary key is indeed named id, then both will do the same thing.
// query by the primary key (usually "id")
$product = $repository->find($id);
// dynamic method names to find based on a column value
$product = $repository->findOneById($id);
$product = $repository->findOneByName('foo');
There is an API here I don't think there is any difference: the two methods, when call the way you call them, do this:
return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id);
But find will be quicker and far quicker in some cases, because it doesn't use the __call magic method, and because find() checks a map of the current unit of work before whereas load() doesn't (see the #todo):
/**
* Loads an entity by a list of field criteria.
* ...
*
* #todo Check identity map? loadById method? Try to guess whether $criteria is the id?
*/
public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(), $lockMode = 0)
So prefer find(), findOneById() is just a less efficient method to do the same thing.
In fact, is not the same thing.
Think about it. If you call "findBy()" you assume you'll receive a collection of entities ( 0, 1 or more than one ). So, to get all results, you'll need to iterate ArrayCollection or just get first ( $result->first() ).
If your query is by a unique key ( As this case ), you can just get unique entity by calling "getOneById()" and you will receive the entity as result.
/**
* Retrieving Product with 'findOneBy'
*/
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:ProductEntity')
->findOneById($id);
/**
* Retrieving Product with 'findBy'
*/
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:ProductEntity')
->findById($id)
->first();
Semantically, the first one it the best.
*TIP
Entity should be called just Product.
Why? Because is under "/Entity" folder ( Almost, should... ), and namespace will contain info about "What is exactly Product"
// query by the primary key (usually "id")
$product = $repository->find($id);
// dynamic method names to find based on a column value
$product = $repository->findOneById($id);
// $foo is any name which you want to find from database
$product = $repository->findOneByName($foo);
It calls the same method in the end.
findByKey('value')
Is basically the same as
findBy(array('key' => 'value'))
Where key is the property of the entity and value is the value of the property.
findById($id)
Is a special case of the above. And so is
find($id)
All of these methods execute the same query in the end. However, there is a difference in
findBy()
and
findOneBy()
Where findOneBy() only returns a single result and findBy will return all the results satisfying the demands.
However, in general it is considered good practice to use DQL queries instead. Consider lazy loading, array hydration, prepared statements, etc.
This is an interesting article on the topic:
Some Doctrine 2 Best Practices
Is the same thing, but I prefer the findOneBy method. It's more clear.
In Doctrine2.0.6, I keep getting an error: "Column VoucherId specified twice".
The models in question are:
Basket
BasketVoucher
Voucher
Basket links to BasketVoucher.
Voucher links to BasketVoucher.
In Voucher and BasketVoucher, there is a field called VoucherId. This is defined in both models and exists with the same name in both DB tables.
The error occurs when saving a new BasketVoucher record:
$basketVoucher = new BasketVoucher;
$basketVoucher->setVoucherId($voucherId);
$basketVoucher->setBasketId($this->getBasket()->getBasketId());
$basketVoucher->setCreatedDate(new DateTime("now"));
$em->persist($basketVoucher);
$em->flush();
I've checked the models and VoucherId is not defined twice. However, it is used in a mapping. Is this why Doctrine thinks that the field is duplicated?
Here's the relevant code - I haven't pasted the models in their entirety as most of the code is get/set.
Basket
/**
* #OneToMany(targetEntity="BasketVoucher", mappedBy="basket")
* #JoinColumn(name="basketId", referencedColumnName="BasketId")
*/
private $basketVouchers;
public function getVouchers()
{
return $this->basketVouchers;
}
BasketVoucher
/**
* #ManyToOne(targetEntity="Basket", inversedBy="basketVouchers")
* #JoinColumn(name="basketId", referencedColumnName="BasketId")
*/
private $basket;
public function getBasket()
{
return $this->basket;
}
/**
* #OneToOne(targetEntity="Voucher", mappedBy="basketVoucher")
* #JoinColumn(name="voucherId", referencedColumnName="VoucherId")
*/
private $voucherEntity;
public function getVoucher()
{
return $this->voucherEntity;
}
Voucher
/**
* #OneToOne(targetEntity="BasketVoucher", inversedBy="voucherEntity")
* #JoinColumn(name="voucherId", referencedColumnName="VoucherId")
*/
private $basketVoucher;
public function getBasketVoucher()
{
return $this->basketVoucher;
}
Any ideas?
EDIT: I've found that the same issue occurs with another model when I save it for the first time. I am setting the primary key manually. The main issue appears to be saving a relationship within an entity.
In this case, I have a field - DraftOrderId - which is used as the primary key on three models. The first model - DraftOrder - has DraftOrderId as a primary key, which is an auto incrementing value. The other two models - DraftOrderDeliveryAddress, and DraftOrderBillingAddress - also use DraftOrderId as a primary key, but it isn't auto incremented.
What's happening is one of the following issues:
If I save the delivery address entity with a draft order id and set it to persist, I get an error: Column DraftOrderId specified twice. Code:
try {
$addressEntity->getDraftOrderId();
} catch (\Doctrine\ORM\EntityNotFoundException $e) {
if ($addressType == "delivery") {
$addressEntity = new Dpp\DraftOrderDeliveryAddress;
} elseif ($addressType == "billing") {
$addressEntity = new Dpp\DraftOrderBillingAddress;
}
$addressEntity->setDraftOrderId($draftOrder->getDraftOrderId());
$em->persist($addressEntity);
}
(It would also help to know if there's a better way of checking if a related entity exists, rather than trapping the exception when trying to get a value.)
If I remove the line that sets the draft order id, I get an error: Entity of type Dpp\DraftOrderDeliveryAddress is missing an assigned ID.
If I keep the line that sets the draft order id but I remove the persist line, and I also keep the lines later on in the code that sets the name and address fields, I don't get an error - but the data is not saved to the database. I am using flush() after setting all the fields - I'm just not using persist(). In the previous examples, I do use persist() - I'm just trying things out to see how this can work.
I can paste more code if it would help.
I think I've fixed it! A couple of findings:
For a primary key that is not an auto-incrementing value, you need to use:
#generatedValue(strategy="IDENTITY")
You also have to explicitly set the mapped entities when creating them for the first time. At first, I was trying to create the address entity directly, but I wasn't setting the mapped entity within the parent model to reference the address entity. (if that makes any sense)
I'm fairly sure it was mostly due to the lack of the IDENTITY keyword, which for some reason was either saying the key wasn't set, or saying it was set twice.