Autogenerated unique field in Doctrine2 entity - doctrine-orm

I have a Doctrine2 entity with two fields, name and URL. I want to generate the URL field automatically according to name field.
E.g. name "John Doe" will have generated URL "john-doe"
I have implemented the generation of URL field in name setter:
public function setName($name) {
$this->name = $name;
$this->url = UrlGenerator::generate($name);
}
I want the URL field to be unique but not the name field. The problem is what to do, when two names converts to same URL.
E.g. name "John Doe" => URL "john-doe", "john doe" => URL "john-doe"
I want to add number (for example the entity DB generated id) to the end of URL field in case when the URL already exists in DB, but I have no idea how to implement this in the entity. At first, I can't use entity manager in entity class to check the collision. The second problem is, that I don't know the next generated id before the entity is persisted to DB and it can't be persisted if it URL collides with existing item.
Thanks for help.

Related

Mongodb Search Query with Populate and regex

I want regex search on mongodb with NodeJS.
First One is Users and Second one is categories. Here I have saved categories id in Users
collection. I want a search Query that filter my records.
Users collection
{_id:'mongo_id', name:"User First",cat_id:2}
{_id:'mongo_id', name:"User Second",cat_id:2}
{_id:'mongo_id', name:"User Third",cat_id:1}
{_id:'mongo_id', name:"User Fourth",,cat_id:4}
Categories
Suppose id is numeric. For demo purpose I have written numeric values.
{_id:'1', name:"Salesman",}
{_id:'2', name:"Carpanter"}
{_id:'3', name:"Plumber"}
{_id:'4', name:"Engineer"}
I have a text input if I will type Carpanter or Carpan(regex) then I want 2 records, or
When I type User Second or second I want 1 Record
var query = {
'name': {
$regex: req.body.name,
$options: 'i'
}
};
innerQuery = {
path: 'category',
select: 'name'
}
Users.find(query)
.populate(innerQuery)
.sort({
'createdDate': -1
})
Your user record looks like this:
{_id:'mongo_id': name:"User First",cat_id:2}
I'll assume the extra : is a typo.
Aside from that, you are attempting to query for title. There is no path title in that record, so you can't query for it.
You're also trying to populate the path category, but there is no field with that name in the user records. The only related field I see is cat_id.
If you want to, for example, query by name and populate the cat_id, you can do something like this:
var query = {
'name': {
$regex: req.body.title,
$options: 'i'
}
};
var innerQuery = {
path: 'cat_id',
model: 'Category',
select: 'name'
};
This is entirely dependent on whether you named your model Category, and also what your schemas actually look like.
(PS, you're also trying to sort by createdDate, but I don't see that field in any of your records, either).

How to peek a record using name rather then using id in ember

So in rails we could find a record by name, id etc, similarly i want to do in ember without making a server request
I have a model called person{id, name}. If i want to peek a record by id i do this:
this.get('store').peekRecord('person', id)
which gives me the record based on id, but now i want to peek a record with a particular name, i tried something like this:
this.get('store').peekRecord('person', {name: "testname"})
which dose not seem to work.
i need a way peek a record using just the name
You can only peekRecord using the unique identifier for the model, which is id property. If you do want to make a request then queryRecord is what you want. but you don't want to make a request so the only choice is peekAll and filter the result,
let allRecord = this.get('store').peekAll('person');
let filteredResult = allRecord.filterBy('name','testname');
//filteredResult is an array of matching records

how to consider annotation groups in a ObjectConstructor

JMSSerializer comes with a Doctrine Object Constructor that does its job, but imagine an Entity with two properties forming a primary key:
UserBase
prop annotated with #ORM\Id and #Serializer\Groups({"1"})
- username
prop annotated with #ORM\Id and #Serializer\Groups({"2"})
- email
User extends UserBase
- other props here, no Id defined.
one property key is excluded by using group=1 while deserializing. The client potentially still sends both email and username. email should not be considered though.
Unfortunately, if you pass the two properties in the body, DoctrineObjectConstructor does not check if something is excluded by deserialization, so it tries to load the Entity from the DB, according to the two values:
foreach ($classMetadata->getIdentifierFieldNames() as $name) {
if ( ! array_key_exists($name, $data)) {
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}
$identifierList[$name] = $data[$name];
}
What I would like to do is taking into account my annotated groups, so as to use the fallbackConstructor in case some property forming the identifier is missing.
As a starter this is a good point: I created my own service, by passing along the annotationDriver. Then, if the property forming the identifier is not associated with the actual group:
$classMetadata = $this->annotationDriver->loadMetadataForClass($metadata->reflection);
$classMetadata->properties //here groups are listed for each property
I can fall back to the fallbackConstructor, as if I had not passed that property in the body
...not so fast! My Entity User extends a UserBase where all my identifier are, so I should take into account hierarchy, possibly in a generic way.
Any hint?
Alright, JMSSerializer's Object Constructor does not consider serialization groups when determing identifiers. Hence, if you include all the IDs in your object, whether they are part of the actual context group or not, they will be counted in.
I created an alternative version of the Object in order to fix this misbehavior (at least for me). Hope it helps

Doctrine and ZF2

I am facing trouble using doctrine join. I can't share my code. But I can tell you scenario.
Please help me to achieve that.
I have created 2 entity. One User and Language.
User table is having foreign key language_id. and Language is master table with id and code fields.
I want to fetch user with some criteria, such a way it returns Language code from Language table as well.
I write join for that but it returns some full object...
Not sure how to fetch corresponding language code from Language table for language_id set in user table
If there is some example you know which can help me then also fine
i have return this in __construct()
$this->languageObj = new ArrayCollection();
when we print it is gives this
[languageObj:User:private] => Common\User\Entity\Language Object
(
[languageId:Language:private] => 1
[languageCode:Language:private] => en
[languageName:Language:private] => English
[languageCode2:Language:private] => User Object
RECURSION
)
I am not able to fetch languageCode from the object
You need methods defined in your entity to return the value from the object. It seems like everything is correct you would just need to grab the value from the entity. Here is an example:
$userEntity->getLanguageObj()->getLanguageId();
Your user Entity would need the getLanguageObj method which you can define like this:
public function getLanguageObj() {
return $this->languageObj;
}
And your Language Entity would also need a getLanguageId method:
public function getLanguageId() {
return $this->languageId;
}
Hope that helps!

Dropdown list in yii from other controller

I want list all objects of my model, and write to file the id of selected model. Using SiteController I render my page but what model I shall use?
$models = myModel::model()->findAll();
$list = CHtml::listData($models, 'id', 'name');
echo CHtml::dropDownList( ???? , $select, $list);
If I get what you're trying to do, You're talking about two models. Like tbl_product => Product and tbl_category => Category.
For demonstration purpose: Say, you want to create a new product and every product must belong to a category, then you might make use of the active dropdown. Using code similar to yours, you can say:
$category = Category::model()->findAll();
$list = CHtml::listData($category, 'id', 'name');
An important thing to note is that CHtml::activeDropDownList() expects different kinds of arguments. The main difference between it and CHtml::dropDownList() is that activeDropDownList( is tied to a Model while dropDownList() isn't.
public static string activeDropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ())
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ())
So, using the example, assuming our Product model has a field called category_id, then the dropdown list would be generated using either:
CHtml::activeDropDownList($model, 'category_id', $list);
or if you've created an Activeform object like this:
$form=$this->beginWidget('CActiveForm');
then you could create the dropDown list like this:
$form->dropDownList($model, 'category_id', $list);
Where $model would be the Product model.
I hope this has been helpful.