Im trying to store a document in a different collection by using Mongodb and symfony2
This is my controller who sets the document into the db.
public function createAction(){
$post = new Post();
$post->setUrl('A Foo Bar');
$post->setTitle('asdf');
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$dm->persist($post);
$dm->flush();
return new Response('Created post id '.$post->getId());}
As you can see, this is the example for the official documentation on DoctrineMongoDBBundle
But the problem is that by default it creates the document into a Collection named as the class, in my case is Post(), so the collection name is Post. I will like to save the document into a Collection named for example charlies_posts or any string as a variable.
It's easy :) In the definition of your document class just use the "collection" parameter.
Here is an exemple :
<?php
namespace MyProject\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* #ODM\Document(
* collection="charlies_posts"
* )
*/
class Post
{
Yeahp, as you say we can define that as a parameter.
Documents\User:
type: document
db: my_db
collection: charlies_post
In this case in the YAML mapping file a different collection may be selected but in my case i want to dinamically set the collection name because i have post related to the user so charlies posts should go to the charlies_post collection and peter posts should go to peter_post collection...
I suppose that there must be a method to set this but i cant find it..
Related
I need to create a custom FE user with some custom fields.
Also, it needs to be assignable through the frontend to different user groups.
You can find my first approach here. Didn't work out that well.
Second approach was to create another extension and follow the guide which is shown here.
First thing I did was to add \TYPO3\CMS\Extbase\Domain\Model\FrontendUser into the Extend existing model class-field for my CustomFEU-model.
Then I created another model which I named FEgroup and I mapped it to the table fe_groups. After that, I connected an n:m relation to the CustomFEU.
When I try to create a new CustomFEU with the new action, it returns a white empty page after submitting the form and no user is being added.
The only strange thing I found was that the /Classes/Domain/Repository/ folder is empty.
TYPO3 7.6.8
Although I didn't edit the files yet, here they are:
Model / Controller / Setup
Did anyone encounter similar problems?
First you need to create the repositories that handle the new user and usergroup models.
Second you try to save the user with $this->customFEURepository->add($newCustomFEU); and the variable customFEURepository does not exist. It would be the best to inject it, it has to be the repository that you should create first. You can inject it like that:
/**
* CustomFEUController
*/
class CustomFEUController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
/**
* #var \Vendor\Feregistration\Repository\CustomFEURepository
* #inject
*/
protected $customFEURepository;
// other code ...
}
Don't forget to clear the system cache after adding inject annotations, otherwise it wont work.
Last but not least i can't see the mapping to the database table for your model. You need to add it to your TypoScript (setup.txt)
config.tx_extbase.persistence.classes {
Vendor\Feregistration\Domain\Model\CustomFEU {
mapping {
recordType = 0
tableName = fe_users
}
}
Vendor\Feregistration\Domain\Model\FEGroups {
mapping {
recordType = 0
tableName = fe_groups
}
}
}
I have a User entity with a hasMany relationship to Profile via a user_id foreign key.
Within my User class I'm trying to create a virtual field so I can access the name property of the linked Profile entity
protected $_virtual = ['profile_name'];
protected function _getProfileName()
{
return $this->profile->name;
}
Whatever I try I get Trying to get property of non-object
I've also tried:
$this->_properties['profile']->name;
$this->profile->_properties['name'];
I know I can get this data by building a query up using Cake\ORM\Table but I has hoping to aovid that.
What am I doing wrong?
p.s. there is definitely linked data between the two tables.
The answer seems to be
That's not how it works
Instead, use 'eager loading' to get the linked entity data. That is to say use the contain method when using find():
e.g.
$users = TableRegistry::get('users');
$query = $users->find('all', ['contain' => ["Profiles"]])
->where(['id' => $userId])
->first();
or on the query object itself:
$query->contain(['Profiles']);
In this example $query will contain Profile entity objects along with the User entity
i.e. $query->profiles is an array of Profile entities
Im trying to create a list of categories that need to be translated and display them as a tree structure. But so far no luck, i got tree structure going but when ever i create new category it adds up to the tree but wont display the name because its being translated with i18n and stores in diffirent table...
$categories_list = $this->Categories->find('treeList')->toArray();
This var stores tree it self with names that i have in Categories Table...
$categories_list = $this->Categories->find('translations')->toArray();
And this one gives me the actual translated categories, anyone has any idea how to combine them, CakePhp3 is a new thing for me and i cant find to much documentation about combining those two behaviors.
In order to get translated fields into tree list you need to add TranslateTrait into Category entity, it should look like this:
Link to CookBook about Translate behaviour and TranslateTrait
/src/Model/Entity/Category.php
<?php
namespace App\Model\Entity;
use Cake\ORM\Behavior\Translate\TranslateTrait;
use Cake\ORM\Entity;
class Category extends Entity{
use TranslateTrait;
//translation field must be accessible
protected $_accessible = [
'translations' => true,
];
}
Then you should stack multiple finder methods to achieve your goal, one for Translation Behaviour and one Tree Behaviour
Link to CookBook about how to stack multiple finder methods
/src/Controller/ArticlesController.php
use Cake\I18n\I18n;
public function foo(){
/***
*
* 1) use translation finder
* 2) use treeList finder
* 3) give to treeList finder the translated value to use in the output array as valuePath param
*
***/
$tree_list = $this->Articles->Categories
->find('translations')
->find('treeList', ['valuePath' => '_translations.' . I18n::getLocale() . '.title'])
->toArray();
}
You can leave I18n::getLocale() as it is to automatically get the tree list in current language or replace it with the language you prefer.
Suppose I have to set column value formula to 1. So how can I do it before persisting. After persisting I should get 1 in database.
$f=1;
$product->setFormula($f);
$em->persist($product);
If I use above line it gives an error
Expected value of type "Nimo\MrmdBundle\Entity\Product" for
association field "Nimo\MrmdBundle\Entity\Product#$basedOn", got
"integer" instead
Here is entity code
/**
* #ORM\ManyToOne(targetEntity="Product")
* #ORM\JoinColumn(name="formula", referencedColumnName="someothercolumn",nullable=true)
**/
private $formula = null;
You have to correct your entity definition first, However here's what you need to do in your controller. This will not work until you make sure your entities are correctly defined. (I can't because I don't know your entity definitions)
$f=1;
$em = $this->container->get('doctrine.orm.entity_manager');
$repo = $em->getRepository('AppBundle:Formula'); //This should be your referred entity
//You can also do findOneByName below
$formula= $repo->findOneById($id); //This should be the primary key of the referred entity NOT 1
$formula->setFormula($f);
$em->persist($formula);
When you are creating a relationship between two entities you can not pass a single value or variable containing a single value.
Entity works on objects. So try to pass the object of some entity or create an object with some value it will work. I also face the same error while passing a single value. Just pass the Object of an entity relationship annotation will picked up the joining column of other entity.
I'm trying to convert data retrieved from my Laravel model to a JSON object as outlined in the Backbone docs.
My problem is that when I encode the data all I get are the public properties and none of the (protected) attributes - the ones I actually want. This is how I go about it:
Controller
$movie = Movie::with('awards.award', 'customAwards.awardCustom', 'cast.person', 'imdb.rottenTomatoes')->find($id);
return View::make('movie')->with(array(
'movie' => $movie
));
View
<script type="text/javascript">
DS.Resources.Movie = {{json_encode($movie)}};
</script>
DS.Resources.Movie output
{
"timestamps":false,
"incrementing":true,
"exists":true
}
If I var_dump $movie I can see the protected attributes like title, year, a cast collection, awards collection etc. How do I access these properties and map them to my JSON object?
In Eloquent, you should use ->toJson() as it correctly only gets the model's attributes, rather than general class properties. Similarly, it'll get your relationships if you used a ->with() I think.
If you don't want certain attributes to come out in the JSON (like password fields) you can specify an array in your class called hidden, see the docs.
You should unprotect them in your model.
Also you should realize you can use ->toJson() on your model instead of json_encode'ing it.