How can i get information form associating doctrine. Here is an example ----
"EntityUser" is JOIN to "EntityApartment".
Many "User" can stay in the same "Apartment".
Now all user have unique id.
All "Apartment" values is been set dynamically, so ApartmentId can be set with many user.
So now if i want to get the Apartment name from "EntityApartment" how can i get that information, because in the "EntityApartment"** there is id, name, value and etc. So how can i get the associations value.
If I understand you correctly, I think you want to create a ManyToOne relashionship between your two entities. Once it is created, you do not have to think about foreign keys, as they are managed by Doctrine. You can simply use your property like you would do with any other. For example :
$user = new User();
$apartment = new Apartement();
$apartment->setAddress('12 xxx street');
$user->setApartement($apartment);
And you can access your user's appartment like this :
// Displays '12 xxx street'
$user->getApartment()->getAddress();
Hope this helps.
Related
how to compaire two tables value and get the result of particular column value from both tables in laravel relationship method? I don't know how to do it with relationship, at the same time I don't know whether it is based on many to many or one to many relationship. at the same time I have to do it with where condition.
here is my mormal query example for my requested code:
select teacher.teacher_name, subject. subject_name from teacher join subject on teacher.subject_token = subject.token where teacher.teacher_token = '1';
As I understood you, you want to get a teacher and his/hers subject?
Teacher::select(['id','name','subject_token'])->with('subject')->where('id', $id)->first()
Explanation: From the teacher's table take a teacher with id $id (id passed to the method) and take his id, name, and subject_token, with this get the subject that has the same token taken from the teacher's table. (Usually, instead of tokens you could use id)
I'm stuck in a problem and I can't find a solution to this, it's annoying me.
I've two tables, one called contacts and the other one called phonebooks and they are linked with a third table called *contacts_phonebooks*, this is a many-to-many relationship summarize below:
contacts: id (pk)
phonebooks: id (pk)
contacts_phonebooks: contactid (fk), phonebooksid (fk)
Pretty simple and clear, and it works.
I'm using Laravel 4 and Eloquent ORM, everythings works fine when I've to fetch it, insert it and delete it but when I need to update a contact I fail miserably. I've a form that has a number of checkboxes that represent all the phonebooks (every checkbox has phonebook[] as name) so when you check one of those the phonebook id will be saved in the *contacts_phonebooks* with the contact id.
The problem is that this is not true! I mean when I run this code:
$contact = Contact::find($id);
$contact->contact_name = Input::get('newCName');
$contact->contact_surname = Input::get('newCSurname');
$contact->contact_email = Input::get('newCEmail');
$contact->contact_phone = Input::get('newCPhone');
$contact->contact_birth = Input::get('newCDate');
$contact->phonebooks()->sync(Input::get('phonebook'));
if($contact->save())
{
return "TEST DONE?";
}
It deletes every row in *contacts_phonebooks* associated with the contact id and save only the new one checked... This is weird I know, I try to explain it better.
I want to update Mr.x and he actually is in "Stackoverflow" phonebook, I want to add him in "Nerd" phonebook so I click on update and I selected "Nerd", the other one is already selected.
When I update him the system deletes the "Stackoverflow" link and save ONLY the "Nerd" phonebook (with the code above) this things driving me crazy because Laravel 4 Doc says that you should use the sync() method in order to update a many-to-many relationship.
I don't how how to solve it, I hope you will understand what's my problem.
Cheers.
The documentation says "The sync method accepts an array of IDs to place on the pivot table. After this operation is complete, only the IDs in the array will be on the intermediate table for the model:"
So what I think you are probably looking for is attach().
$contact->phonebooks()->attach(Input::get('phonebook'));
Then you will have to use detach() to remove him from the other.
As stated in the docs: The sync method accepts an array of IDs to place on the pivot table.
Your pivot table should be named
contact_phonebook
and it specifies that in Laravel's documentation.
I've just read the Redbean Documentation, and it's awesome! But I have some questions before I proceed.
I want to create a User data structure composed in a particular way. On the site I'm working on, I have three types of users:
The professionist
The company (with a contact person to represent the company)
The student
I think the best way to implement this user data structure is to have a person table (that has all the common data), and other three tables for the professionist/company/student unique data.
A user on my site will be then a composition of Person <---> Professionist for example, but this is not a one-to-many relationship (i think).
How can I achieve this in MySQL? Or better in Redbean?
A lot of developers tend to make a butt load of tables to do what they want, and although this does work, it can get extremely complicated (especially if other developers need to do updates on your code).
What I would do is create a table called "users" and then have a row called 'type' which will allow you to assign that specific user either professionist, company or student.
so it would look something like this:
$user = R::dispense('users');
$user->name = 'Name Goes Here';
$user->type = 'Student';
R::store($user);
And you can add whatever other data you need and should be dynamic but this gives you a simplified version of what you would need.
The only other thing you can do is create a relation table that specifies the different account types, and then when you add in your user info in the "users" table just replace $user->type with $user->type_id and relate the typeid with the inserted user, but to me that just seems like more work.
I have 3 Entities Users, UserProfile and Staffs
The User profile is linked to Users table through user id
The Staff table is linked to userprofile using the userprofileid
Admin create the user profile and generate the registration no, username and password.
I want add a user record to the users table then add the user profile and then add the profile id to the staff table.
I want to persist three entities sequentialy
I tried to create an instance for the Users like
$this->userent->setUsername('xxx');
$this->em->persist($this->userent);
$this->em->flush();
then:
$this->profileent->setFirstname('xxx');
$this->em->persist($this->profileent);
$this->em->flush();
Basically a form is shared among three entities and I want to insert into three tables sequentially,
Updated
Apart from users entity i have a usertype entity linked to users...i want to persist only the foreign key. i have
setUserType(Usertype $userType) method an instance of the user_type entity in users
when i do
$this->userent = new Users();
$this->userent->setUserType($this->em->getRepository('\Campus\Entity\Usertype')->findByUserType("admin"))
i get the error
Argument 1 passed to Campus\Entity\Users::setUserType() must be an instance of Campus\Entity\Usertype, array given
if i pass the value of the array which is an instance of Usertype
i get an error saying need an array for the ArrayCollection..help please!!!
Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in D:\xampp\htdocs\zend\library\Doctrine\ORM\UnitOfWork.php on line 406 defined in D:\xampp\htdocs\zend\library\Doctrine\Common\Collections\ArrayCollection.php on line 46
Think less about the database, and more about your objects. That's the whole point of doctrine.
You want something like this:
<?php
// create some entities
$user = new Entity\User();
$user->setUsername('userman');
$profile = new Entity\UserProfile();
$profile->setFirstname('joe');
$profile->setLastname('smith');
$staff = new Entity\Staff();
$staff->setSomething('value-for-something');
// associate those entities together
$profile->setStaff($staff);
$user->setProfile($profile);
// assuming you have set up cascade={"persist"} on your associations
$this->em->persist($user);
// if you haven't set up cascade={"persist"}, you will need to call persist on each entity:
// $this->em->persist($profile);
// $this->em->persist($staff);
$em->flush();
So, the basic idea is you build up your objects, get them into Doctrine's Unit-of-Work (by calling persist() and maybe having some cascades set up), then write them all to the database in a single transaction by calling flush()
You can persist each entity, and then flush() one time at the end.
If you have relations between your entities, you can check the following annotation
cascade={"persist"}
"Cascades persist operations to the associated entities."
Have a look at the documentation here : http://docs.doctrine-project.org/en/2.0.x/reference/working-with-associations.html#transitive-persistence-cascade-operations
I have a tables called users, countries, and countries_users.
The documentation states that to delete a simple relationship you perform:
// Get user foo
$u = new User();
$u->where('username', 'foo')->get();
// Get country object for Australia
$c = new Country();
$c->where('name', 'Australia')->get();
// Delete relation between user foo and country Australia
$u->delete($c);
This would remove the corresponding row from the countries_users table.
My question is, what if I have no relevant Country() object to construct?
If countries and users are a one-to-many relationship, then certainly knowing the username attribute is enough to disassociate him with a country.
All the delete functions seem to require at least two objects... What is the best way to accomplish the deletion of this type of relation using the DataMapper ORM functions?
"All the delete functions seem to require at least two objects"
Not totally true, a delete() can be preformed on a single object without the need to explicitly delete the object's relationships, it is handled automatically.
From the user guide:
Note: When you delete an object, all its relations to other objects will also be deleted. Free house cleaning! :)
In addition, you may use a column in the users table for the country id instead of a separate countries_users table for relationships, assuming it is a one(country)-to-many(users) relationship.
My question is, what if I have no relevant Country() object to construct?
Then you don't have to worry about anything. If there are no relationships to delete, attempting to delete them will not cause any harm.
There is a relationship to delete! I want to pass the user_id into my Controller and disassociate him with a country in the countries_users table. To accomplish this using the documented functions, I would have to also pass in the country_id... Which IMO is irrelevant for this operation.
You don't have to look up the country id unless you specifically want to delete a particular relationship. In your case, you're working with a relationship where a user can only have one country, so you don't need to specify which related country to delete. Here are two options off the top of my head:
Assigning a new country (removes the previous one)
$c = new Country();
// Get all countries named "Wonderland"
// Usually we'll use an id instead, there could theoretically be more than one
$c->where('name', 'Wonderland')->get();
$user->save($c);
Just delete all related countries (there is only one of course)
$c = new Country();
// Get all countries
$c->get();
$user->delete($c); // You may need $c->all here
If we were working with a many to many relationship, you would of course have to know which ones to delete, but since there is only one - deleting them all is sufficient.
Believe it or not, I was unable to remove the relationship using the code Wesley has provided.
However, this seemed to work:
$u = new User();
$u->where('id', $id)->include_related('country', 'id', TRUE, TRUE)->get();
$c = new Country();
$c->where('id', $u->country->id)->get();
$c->delete($u);