Symfony2 forms embed mongodb document into sql entity - forms

I need little help :). Here is the situation. I am using symfony2 + FOSUserBundle, I made my forms custom, so far so good. I have User registration with user information in the custom registration form (like first name, last name, birth date etc). Now I decided that it will be more practical to make the user info to be stored in mongodb as document (as I probably will add more information to users later). I built the user info form, and successfully embedded it to the user form. Now the problem is that I cannot set Document object inside Entity object - symfony tells me that the object must be an Entity.
/**
* Acme\UserBundle\Entity\User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User extends BaseUser
{
/**
* #Assert\Type(type="Acme\UserBundle\Document\UserInfo")
*/
protected $userinfo;
I want to ask, what is the proper way to do this ? Sure I can get the needed information form the request as an array and fill in the user info object ... but it looks ugly and wrong :) so how it must be done ? Thanks.

I assume you extends the entity class
FOS\UserBundle\Entity\User
there is a document class provided by the bundle
FOS\UserBundle\Document\User
You could extends this one

Related

How to add a field which is an array of different models

We defined in TYPO3 9.5 this field in a model:
/**
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\GeorgRinger\News\Domain\Model\News>
* #TYPO3\CMS\Extbase\Annotation\ORM\Lazy
*/
protected $txIctivigoNoticesFrom;
/**
* Get notices from
*
* #return \GeorgRinger\News\Domain\Model\News[]
*/
public function getTxIctivigoNoticesFrom()
{
return $this->txIctivigoNoticesFrom;
}
/**
* Set notices from
*
* #param \GeorgRinger\News\Domain\Model\News[] $txIctivigoNoticesFrom
*/
public function setTxIctivigoNoticesFrom($txIctivigoNoticesFrom)
{
$this->txIctivigoNoticesFrom = $txIctivigoNoticesFrom;
}
But now we want to allow this is an array of not only news records, but pages and sys_file_metadata records as well. How could we achieve that?
We tried to instantiate an abstract entity between <>, but it's not possible.
TCA field is defined properly as a group type, and it saves relations to these three tables.
There is no ready-to-use functionality in extbase to resolve such mixed references into objects. In the database field, the content is a comma separated list of identifiers, which are of the form TABLENAME_UID, like tt_content_1670,tt_content_1672,pages_123. So your only choice would be to receive those values as string(s) and then load the objects yourself.
With this solution, keep in mind, that there are no default extbase models for the core elements like tt_content, page and so on. So you will either have to use something like https://github.com/georgringer/news/blob/master/Classes/Domain/Model/TtContent.php and https://github.com/georgringer/news/blob/master/Classes/Domain/Repository/TtContentRepository.php and/or write the corresponding models and repositories yourself!
I you don't need extbase objects, but just the plain database data for some reason, you can also load this directly from the database.

Get UID of object after adding to the repository

So i'm trying to set up my first extension with Typo3 and i'm struggling very much. I want to set up an extension which handles reclamations from customers.
After submitting the input values i'll store the new customer in the database and directly after this step i'll get it back from the database to see which uid he has, to store the uid from the customer in the reclamation.
So i wan't to override the current $customer-variable with
$customer = $this->customerRepository->findByName($name);
The returned result is not really an object of customer even var_dump is saying it is an customer-object. I can't call the function
$customer->getUid()
to get the current uid of this new customer. But i need the uid of the customer in my reclamation-object - how do i do that?
Next problem: every query i ll do to the db like
->findAll(), findByIdentifier($identifier)
is null.
I don't know why. It seems that he can't find the storagePid, but i've set up my TypoScript correctly.
I can only get a query when i add
$query->getQuerySettings()->setRespectStoragePage(FALSE);
any ideas where the dog is buried in this case?
Thank you very much and sorry for my bad english :P
Your initial question is hard to answer without more details. What is the relation between a customer and reclamations? If a customer can have multiple reclamations, it would be good to have an 1:n relation between customer and reclamations. In that case, you can just do $customer->addReclamation($reclamation) and don't need to take care about user UIDs.
As for your repository call, the problem is that your call gets you a QueryResult containing all matching objects. If name is really a unique property, you can do
$customer = $this->customerRepository->findOneByName($name);
This looks for all customers with name equals $name (which should be only one) and returns the first one, so you get back a Customer object.
But this is not really necessary, too: If you persist all changes after adding the customer, you can get its UID:
$this->customerRepository->add($customer);
$this->persistenceManager->persistAll();
// Returns the customer uid
$customerUid = $customer->getUid();
The persistenceManager can be injected like this:
/**
* #var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
* #inject
*/
protected $persistenceManager;

How to use two collections to authenticate zfcuser?

I use Doctrine and MongoDB ODM modules at my zf2 application. ZfcUser is used for authorization.
Is there a way to use two collections, say users and clients to authenticate via zfcuser+doctrine? I am curious, if there is a way to combine two mongo collections into one to use combined for authentication?
You do not need to merge the users into one collection as you can have multiple 'authentication adapters' (see ZfcUser\Authentication\Adapter\Db for an example)
These are defined within global config file: zfcuser.global.php
Each of my adapters are run in order of priority until one returns a successful authentication result.
For example; I have the following configuration for Users and Candidates entities.
/**
* Authentication Adapters
*
* Specify the adapters that will be used to try and authenticate the user
*
* Default value: array containing 'ZfcUser\Authentication\Adapter\Db' with priority 100
* Accepted values: array containing services that implement 'ZfcUser\Authentication\Adapter\ChainableAdapter'
*/
'auth_adapters' => array(
50 => 'JobboardCandidate\Authentication\Adapter\CandidateDatabaseAdapter',
75 => 'JobboardUser\Authentication\Adapter\UserDatabaseAdapter',
//100 => 'ZfcUser\Authentication\Adapter\Db', [this is the default]
),
As I understand from your question you want to get one collection with two different document types so that you can use this collection for authentication.
If you use doctrine Inheritance mapping you can have two different classes and resolve them in one collection.
In this case your Client class would extend your User class. If you would use the findAll method in your UserRepository you would get both the clients and the users in one Collection
This will help you achieve what you want:
<?php
namespace MyProject\Model;
/**
* #Document
* #InheritanceType("SINGLE_COLLECTION")
* #DiscriminatorField(name="discriminator", type="string")
* #DiscriminatorMap({"user" = "User", "client" = "Client"})
*/
class User
{
// ...
}
/**
* #Document
*/
class Client extends User
{
// ...
}
And then
$userRepository->findAll();
Read more on inheritance mapping here in the Doctrine documentation

unique index with doctrine mongodb odm

i'm doing a registration system for my site and want to prevent duplicate registrations with the same email address.
the declaration of the user class looks like this:
/**
* #Document
*/
class User extends BaseEntity
{
private
/**
* #Id
*/
$id,
/**
* #String #Index(unique=true)
*/
$email
;
}
but whenever i save a user with the same email, no exception is raised and i get a duplicate.
i found somewhere that i need to do $documentManager->flush(array('safe'=>true)); but that doesn't help.
How can I achieve what i need? Thanks
I had a similar problem. The index is not being created by Doctrine as you can see by typing the following in the mongo console:
db.system.indexes.find()
I had to create my index directly in mongo per these instructions. After that duplicates won't be created.
However Symfony2/Doctrine doesn't seem to throw any exceptions, the insert just fails silently. Mongodb DOES alert you of the failed insert if you do it directly in the console.
--edit: An exception is thrown when array('safe'=>true) is used as a parameter to flush() as per the original post.

same password for multi-users in zend

I have the following scenario in zend framework:
Data
Table of students
Table of classes, which contain many students each.
Table of assignments, each of which is assigned to a class and given a password
I want students to be able to access an assignment given that assignment's id and shared password, but for the application to note which student signed in to the assignment. Zend_Auth however expects one table to contain both the username and the password, but in my situation the username is in the students table, and the password is in the assignments table.
Can anyone suggest a good way of handling the student login where they can all share one password. A way to authenticate with just a username and no password would work, as then I could do the password check in a separate conditional.
I think your best bet would really be to just write your own adapter. Something like this would most likely work:
class MyAuthAdapter implements Zend_Auth_Adapter_Interface
{
protected $_username;
protected $_password;
protected $_assignment_id;
/**
* Sets username, password, and assignemnt ID for authentication
*
* #return void
*/
public function __construct($username,$password,$assignment_id)
{
$this->_username = $username;
$this->_password = $password;
$this->_assignment_id = $assignment_id;
}
/**
* Performs an authentication attempt
*
* #throws Zend_Auth_Adapter_Exception If authentication cannot
* be performed
* #return Zend_Auth_Result
*/
public function authenticate()
{
// logic here to check everything out and erturn a new Zend_Auth_Result
}
}
Shared passwords are a really bad idea. If you share the password, then another student need only learn an id -- typically not a highly secured piece of information -- to access the resource as the other student. A better solution would be to use a role to control access to assignments and put the students who need access to the assignment in the role. This way each student can still have access to the assignment and retain their own id/password pair. See the Zend documentation for information on roles and how to use them.