One review per user in magento - magento-1.9

I have product's review on product page now. But logged-in customer gives review more then once. I need only one review from one customer, not more than that.
Can you please suggest??
Thanks in Advance.

You just have to modify the review form as per the customer who is logged in. If they have already given review, then dont display. You can take help of this:
I looked into Magento\Review\Model\ResourceModel\Review\Collection and found this method.
Here you can find this method :
/**
* Add customer filter
*
* #param int|string $customerId
* #return $this
*/
public function addCustomerFilter($customerId)
{
$this->addFilter('customer', $this->getConnection()->quoteInto('detail.customer_id=?', $customerId), 'string');
return $this;
}
So in the above $customerId, pass the customer ID from the customer session array and just use an if loop for this.
Hope this helps.

Related

Laravel Join Query returning empty column

I have a member table with some entries. Each member can create a user account in laravel's users table.
They each have a field called "person_id" and that's how the connection is made.
I have a search that returns a list with all of them. I have a checkbox "Search only registered" that means it returns only members that have users account, otherwise if the check doesn't check, return a mix with all of them.
The thing is, no matter if the checkbox is checked or not, the person_id must be pulled for each one.
if($reg == 'on') {
$Members = $Members->rightJoin('users', 'users.person_id', '=', 'members.person_id');
}
else {
$Members = $Members->leftJoin('users', 'users.person_id', '=', 'members.person_id');
}
I tried with leftJoin but person_id comes empty
at first look if you are using Eloquent i can tell you are missing the "->get();" at the end of each query.
Hope this helps.
Use relation in member model:
public function user()
{
return $this->belongsTo('App\User', 'person_id', 'person_id' );
}
public function getMemberWithUser()
{
return $this->select('*')->with('user')->get()->toArray();
}
and use (new Member)->getMemberWithUser(); in controller. That will return you member detail with user.
Neverming guys I found it out.
Most members don't have yet a user account, only 2. And the select wasn't specifying which table to take the person_id from. And with most members missing an user account, it was trying to get it from users.
I did this:
$Participants = Member::select(
'members.first_name',
'members.last_name',
'members.person_id',
'members.email'
);

Laravel get items based on a many to many relation

How do I query in Laravel Eloquent the following criteria
having project, employees tables and a many to many binder project_member table, I want to get projects where a project is owned by logged in user as a team member.
in my Project model I have
/**
* #return mixed
*/
public function members()
{
return $this->belongsToMany('App\User', 'project_team', 'id', 'project');
}
and user table
/**
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function team()
{
return $this->belongsToMany('App\Project', 'project_team', 'employee', 'project');
}
and I want to get all projects where logged in user is a member of a given project

Expanding this method to write to the database

Hi I followed a tutorial to implement a friend system. It all works find, but I need to post other columns to the row that just the id's. How would I expand that.
This is the method that is accessed when the add friend button is clicked
public function getAdd($id){
$user = User::where('id', $id)->first();
//After passing all checks. Add other account
Auth::user()->addFriend($user);
echo "Sent";
}
AddTenancy Method
public function addFriend(User $user){
$this->friendsOf()->attach($user->id);
}
I assume the relationship is many-to-many between users. And you need to add additional data to the pivot.
Here's how you'd do that:
public function addFriend(User $user){
$this->friendsOf()->attach($user->id, ['another_col' => 'some data']);
}
Replace 'another_col' and some data with your column and your data. You can also add more than 1 column into the array.

Query same table with pivot table

I have following tables:
users - id, username, etc.
conversations - id, private, etc.
conversation_user - id, user_id, conversation_id
There are two records in the conversation_user table for each open/started conversation (could be more if a conversation is multi user).
One for user as recipient/recipients, another for a user as sender. Let's say: user_id=1, conversation_id=1 and user_id=2, conversation_id=1.
How can I select all users/usernames I have an open conversation with. All the participants, to be exact.
I assume I need to get a conversation id's I'm a participant at and then do a reverse lookup for other users in the conversation. But no luck so far... This reverse lookup is where I got stuck.
I'm playing with Laravel 5.3 and MySQL.
First of all, you need a has_started_conversation kind of boolean on your pivot table, conversation_user.
As noted in the docs, you need to define the relationships between your Conversation and User models.
User
class User extends Model
{
public function conversations()
{
return $this->belongsToMany('App\Conversation')->withPivot('has_started_conversation');
}
}
Conversation
class Conversation extends Model
{
public function users()
{
return $this->belongsToMany('App\User')->withPivot('has_started_conversation');
}
}
Then
// get a users conversations
$conversations = \App\User::first()->conversations;
// select a single conversation, or maybe multiple with a for loop?
$important_conversation = $conversations->random();
// get the participants of that conversation
$participants = $important_conversation->users;

symfony 2 many to many relation crud

I've created a many to many relation and generated crud via command line. I have Users and Groups.
USER
/**
* #ORM\ManyToMany(targetEntity="Grup", mappedBy="users")
* #ORM\JoinTable(name="user_has_grup",
* joinColumns={#ORM\JoinColumn(name="grup_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
protected $grups;
Grup
/**
* #ORM\ManyToMany(targetEntity="User", inversedBy="grups")
* #ORM\JoinTable(name="user_has_grup",
* joinColumns={#ORM\JoinColumn(name="grup_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
protected $users;
When i create user group show up but i can't assign user to group. Still when i go to edit Group i can assign User to it and its works well.
What do I need to change, if i want to be able do it in both directions? Is there any Doctrine Entity change or in controller ?
Don't know if it's your problem because not enough code...
But i think this can help : Symfony2-Doctrine: ManyToMany relation is not saved to database
And official documentation :Owning and Inverse Side on a ManyToMany association