how to pass the name attribute insted of id in mongoid? - overriding

i am using mongoid database, i am trying to override the id attribute in url to name, but i am getting error
my model is like this
class Product
include Mongoid::Document
field :name, type: String
field :price, type: BigDecimal
field :released_on, type: Date
field :_id, type: String, default: -> { name.to_s.parameterize }
validates_presence_of :name
embeds_many :reviews
end

Related

Doctrine ORM value object for mutiple database fields

I have the following ORM:
AppBundle\Domain\ValueObject\Date\Birthday:
type: embeddable
fields:
birthday:
column: fecha_nacimiento
type: datetime
And I want to use this value object in two tables, the first is "User" table:
AppBundle\Domain\Entity\User\User:
type: entity
table: usuarios_web
repositoryClass: AppBundle\Infrastructure\Persistence\Doctrine\Repository\User\UserDoctrineRepository
embedded:
birthday:
class: AppBundle\Domain\ValueObject\Date\Birthday
columnPrefix: false
But then I want to use the same birthday on another table who's birthday field is not "fecha_nacimiento", is different.
It's possible to map the DB field in the emmbedded instead of the embeddable?

FOSElasticaBundle sort not working

The configuration of my elastica type looks like following:
acme_article:
mappings:
title: {type:string, index_analyzer:acme_analyzer}
content: {type:string, index_analyzer:acme_analyzer}
slug: ~
media: {type:string, index_analyzer:acme_analyzer}
categories:
type: "object"
properties:
name: ~
id: ~
instance:
type: "object"
properties:
name: ~
created_by: ~
created_at: ~
I have Repository class which extends FOS\ElasticaBundle\Repository and everything works well except sorting.
$query = new \Elastica\Query();
$query->setSort(array('created_at' => array('order' => 'desc')));
return $this->find($query);
Getting some irelevant result, totally without order. Then, I tried to add model id in index and try to sort by id but also without success.
Solution is to set {type:date} on created_at field. In that case ES sorting by timestamp and everything is ok.
Sorting by id is not working because in some way categories.id override main id and then I got results ordered by category.id instead entity id.

Self joins in Batmanjs

How can I implement the self-joins in Batmanjs?
In rails, as found here, it goes like this:
class Employee < ActiveRecord::Base
has_many :subordinates, class_name: "Employee", foreign_key: "manager_id"
belongs_to :manager, class_name: "Employee"
end
My current Batmanjs equivalent model looks like this:
class Employee extends Batman.Model
#resourceName: 'employees'
#storageKey: 'employees'
#persist Batman.LocalStorage
#has_many 'subordinates', name: "Employees", foreignKey: "manager_id"
#belongs_to 'manager', name: "Employee"
I think that should work, if you just switch:
has_many/belongs_to => hasMany/belongsTo
name: "Employees" => name: "Employee".
Also, you may have to add an encoder for id with the LocalStorage adapter. LocalStorage converts the value to a string, but batman.js expects an integer, so you have to coerce it back to integer in the encoder.
Here's an example of self-joins (you can copy-paste the encoder from there, too):
http://jsbin.com/cukapedo/18/edit
Pasted here for posterity:
class App.Color extends Batman.Model
#resourceName: 'color'
#persist Batman.LocalStorage
#encode 'name', 'source_color_id'
# required for numbers in localStorage:
#encode 'id',
encode: (val) -> +val
decode: (val) -> +val
#hasMany 'child_colors', name: 'Color', foreignKey: 'source_color_id'
#belongsTo 'source_color', name: 'Color'

No entity manager defined for class DateTime

I have started using the Sonata Admin bundle and I was following the example on how to map an entity with the bundle to create an administrative interface.
I created an entity called Post and this is the configuration yml file:
Emiliano\PostsBundle\Entity\Post:
type: entity
table: null
repositoryClass: Emiliano\PostsBundle\Entity\PostRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
column: Title
lenght: 100
published:
type: boolean
column: Published
publishingDate:
type: datetime
column: Publishing_Date
nullable: TRUE
lifecycleCallbacks: { }
Then in my Admin class I have the configureFormFields method:
protected function configureFormFields(FormMapper $formMapper) {
$formMapper->add('title', 'text')
->add('published', 'checkbox', array('required' => false))
->add('publishingDate', 'sonata_type_model_hidden');
}
I found the sonata_type_model_hidden on the sonata admin documentation. What I would like to achieve is to programmatically handle the publishing date (e.g. set the date only if the checkbox published is checked) hiding the implementation to the user.
Everything works fine for create, delete and read, when it comes to modify an entity I get this message in the stacktrace:
No entity manager defined for class DateTime
In sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php at line 214
If I show the field everything works fine, I tried also to use:
->add('publishingDate', 'hidden');
without success.
What is exactly the problem here? Is it because Sonata Admin tries to fill a form with the entity values and for publishingDate there's a DateTime while in the form specification I wrote a sonata_type_model_hidden? If so, how can I circumvent this?
sonata_type_model_hidden isn't just hidden field genereator, according to documentation:
sonata_type_model_hidden will use an instance of ModelHiddenType to render hidden field. The value of hidden field is identifier of related entity.
If I understand your problem, You want to set publishing date only when field published == true
You could use entity preSave/preUpdate lifecycle callback for eaxmple
public function preSave()
{
/**
* Check if item is published
*/
if($this->getPublished()) {
$this->setPublishingDate(new \DateTime());
} else {
$this->setPublishingDate(null);
}
}
and remove publishingDate field from SonataAdmin form.

Update an Entity with Unique Key - Insert instead of update

I have this method in my service:
public function updateCountry($p_entity) {
var_dump($p_entity->getId()); //return 3 (as expected)
var_dump(get_class($p_entity) ); //return Country (as expected)
$this->em->persist($p_entity);
$this->em->flush();
}
I call it by
$country = $em->getRepository('blabla')->find(3);
$my_service->updateCountry($country);
But the request generated is
Doctrine\DBAL\DBALException: An exception occurred while executing
'INSERT INTO country (code, label , created_at, updated_at)
VALUES (?, ?, ?, ?)' with params ["EN", "England", "201
3-08-23 00:00:00", null]:
I have a unique doctrine constraint
this is my country.orm.yml
To\Bundle\Entity\Country:
type: entity
table: country
repositoryClass: To\Bundle\Entity\CountryRepository
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
code:
type: string
length: 255
unique: true
nullable: false
Why have I an insert request generated instead of the update one?
Can you persist new entity at all?
If you can, try merging your object instead:
public function updateCountry($p_entity) {
var_dump($p_entity->getId()); //return 3 (as expected)
var_dump(get_class($p_entity) ); //return Country (as expected)
$this->em->merge($p_entity); // MERGE not PERSIST
$this->em->flush();
}
From official docs:
If X is a new entity instance, a new managed copy X’ will be created
and the state of X is copied onto this managed instance.
So, basically, EntityManager::merge can be used to both persist newly created object and merge exsistent one...
You shouldn't persist existing in DB entity, only new one.
So if you get entity from DB and want to update it
you should
$entity->setName()
$em->flush

Resources