Laravel Custom Pivot Table relationship and eager loading? - database

I am having problems with creating a schema / model for one of my projects and would like to get some help here.
I have 3 tables currently : accessories , products and a pivot table product_accessory
<?php
Schema::create('accessories', function(Blueprint $table)
{
$table->increments('id');
}
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
}
Schema::create('product_accessory', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('accessory_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('accessory_id')->references('id')->on('accessories');
}
Now problem is I need to add another product type 'adaptors' that would ultimately depend on the pivot table relation, that is, adaptors need to relate to both a product and accessory...
UPDATE
Here is how my current product_accessory_adaptor table is
Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_accessory_id')->unsigned();
$table->foreign('product_accessory_id')->references('id')->on('product_accessory');
}
This way, i can have many adaptors relating to a product and accessory. My question is how do i model this relation in eloquent?
Here's what i have now:
Custom pivot model :
class ProductAccessory extends Pivot {
protected $table = 'product_accessory';
public function product()
{
return $this->belongsTo('Product');
}
public function accessory()
{
return $this->belongsTo('Accessory');
}
public function adaptors() {
return $this->hasMany('Adaptor', 'product_accessory_id');
}
}
Product and Accessory model
class Accessory extends Eloquent {
public function products()
{
return $this->belongsToMany('Product', 'product_accessory', 'accessory_id', 'product_id')->withPivot();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
if ($parent instanceof Product) {
return new ProductAccessory($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
public function adaptors()
{
return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'accessory_id', 'product_accessory_id');
}
}
class Product extends Eloquent {
public function accessories()
{
return $this->belongsToMany('Accessory', 'product_accessory', 'product_id', 'accessory_id')->withPivot();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
if ($parent instanceof Accessory) {
return new ProductAccessory($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
public function adaptors()
{
return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'product_id', 'product_accessory_id');
}
}
Adaptor model:
class Adaptor extends Eloquent {
protected $table = 'product_accessory_adaptor';
public function productAccessory() {
return $this->belongsTo('ProductAccessory');
}
}
Update
Now the schema and model is setup. However there are issues with using hasManyThrough relations. In addition, any way to do eager loading in this case for the pivot relation , i.e Adaptor?
Note
The error that occurs when i make a call to adaptors() on either the Product or Accessory model is
Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model, none given, called in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 872 and defined

That's your pivot:
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
// you don't need to call it ..Pivot, just my suggestion
class ProductAccessory extends Eloquent {
protected $table = 'product_accessory';
public function product()
{
return $this->belongsTo('Product');
}
public function accessory()
{
return $this->belongsTo('Accessory');
}
public function adaptors()
{
return $this->hasMany('Adaptor', 'product_accessory_id');
}
}
// Product model
public function adaptors()
{
return $this->hasManyThrough(
'Adaptor', 'ProductAccessoryPivot', 'product_id', 'product_accessory_id'
);
}
// Accessory model
public function adaptors()
{
return $this->hasManyThrough(
'Adaptor', 'ProductAccessoryPivot', 'accessory_id', 'product_accessory_id'
);
}
Now example usage:
$product = Product::first();
$product->adaptors; // collection of all adaptors for given product
$product->adaptors->first()->accessory; // accessory for single adaptor
$product->accessories; // collection of accessories, each with your custom pivot, so:
$product->accessories->first()->adaptors; // collection of adaptors for given product-accessory pair
... and more, try it

Related

Symfony entity set array as single entity elements

i have problem, i would to add prototype array to database but this show me this error:
Expected argument of type "AppBundle\Entity\Tag", "array" given
...
Post ->setTag (array(array('value' => 'test'), array('value' => 'tess')))
here is my setter for tag:
public function setTag(\AppBundle\Entity\Tag $tag = null)
{
$this->tag = $tag;
return $this;
}
I Have two entities with relation, here relation:
class Post
{
/**
* #ORM\ManyToMany(targetEntity="Tag", inversedBy="post")
* #ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*/
private $tag;
public function setTag(\AppBundle\Entity\Tag $tag = null)
{
$this->tag = $tag;
return $this;
}
}
and tag:
class Tag
{
/**
* #ORM\ManyToMany(targetEntity="Post", mappedBy="tag")
*/
private $post;
}
Source:
http://snipet.co.uk/kR
http://snipet.co.uk/gcf
http://snipet.co.uk/0VI
You're trying to model a bidirectional many-to-many relation between Post and Tag.
So, first of all, your getters need to return a collection of objects, and your setters need to accept a collection of objects - not only one single object as in your code (your setTag method accepts a parameter of type Tag - but you need an array-like parameter).
Secondly, the Doctrine framework does not work with simple PHP arrays, but with implementations of \Doctrine\Common\Collections\Collection.
Next, you need to initialize your collection fields in the constructors of your entity classes with an implementation of the Collection class - you can use \Doctrine\Common\Collections\ArrayCollection.
So your entity classes should look rather like this:
/**
* #ORM\Entity
*/
class Post
{
/**
* #ORM\ManyToMany(targetEntity="Tag", inversedBy="posts")
* #ORM\JoinTable(name="posts_tags")
*/
private $tags;
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getTags()
{
return $this->tags;
}
public function setTags(\Doctrine\Common\Collections\Collection $tags)
{
$this->tags = $tags;
}
}
/**
* #ORM\Entity
*/
class Tag
{
/**
* #ORM\ManyToMany(targetEntity="Post", mappedBy="tags")
*/
private $posts;
public function __construct()
{
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getPosts()
{
return $this->posts;
}
public function setPosts(\Doctrine\Common\Collections\Collection $posts)
{
$this->posts = $posts;
}
}
I strongly advise you to read once again the documentation of the Doctrine framework, how to annotate your entities, and how to model relations: http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html

Retrive data based on pivot table in laravel

I have those tables in my database
Translation
id
translation_key_id
content
Language
id
code (it would be "eng" or "ger" or "fre")
translation_language
id
translation_id
language_id
Now the models are
class Language extends Eloquent {
protected $fillable = array('id','code');
protected $table = 'language';
private $rules = array();
public function translation()
{
return $this->belongsToMany('translation','language_translation');
}
}
class Translation extends Eloquent {
protected $fillable = array();
protected $table = 'translation';
private $rules = array();
public function language()
{
return $this->belongsToMany('language','language_translation');
}
}
Now i want to retrieve those data which have transkation_key_id = abc (as for example ) and also with code = "eng"
How can i do that?
First of all I don't see the need for pivot table here.
Show me why you need that, or change your relation to belongsTo. You can link translation with language using id (1) or code (2), which is unique obviously, right? So here it goes:
table translations: id, key, content, language_id (or language_code)
// Translation
public function language()
{
// option 1
return $this->belongsTo('Lanugage');
// or 2:
// return $this->belongsTo('Lanugage', 'language_code', 'code');
}t
then
// option 1
Translation::where('key', 'whatever')->whereHas('language', function ($q) {
$q->where('code', 'eng');
})->first();
// option 2, even easier w/o any join needed
Translation::where('key', 'whatever')->where('language_code', 'eng')->first();
You've built relationships incorrect
try this
class Language extends Eloquent {
protected $fillable = array('id','code');
protected $table = 'language';
private $rules = array();
public function translation()
{
return $this->belongsToMany('Translation','translation_language');
}
}
class Translation extends Eloquent {
protected $fillable = array();
protected $table = 'translation';
private $rules = array();
public function language()
{
return $this->belongsToMany('Language','translation_language');
}
}

CakePHP exception logic

I have a controller where I throw a custom exception and I have a custom exception renderer class, which extends the basic exception renderer.
Now when I throw the exception, I'd like to do some cleanup with the stuff, that went wrong and after that render a custom error page.
class AppExceptionRenderer extends ExceptionRenderer {
public function invalidCall($error) {
$this->controller->render('/Errors/invalid_call');
$this->controller->response->send();
}
public function incompleteCall($error) {
$this->controller->render('/Errors/incomplete_call');
$this->controller->response->send();
}
}
The rendering works well so far. But where should I put the logic for the cleanup things?
In the exception itself? In the renderer? In the controller before throwing the exception?
Well, as so often there are many ways to skin a cat, but I'd say in order to stay DRY, for easy testing, and in order to stay in compliance with the recommended fat model concept, you should put the logic in the model.
And in order to decouple cleanup and exception handling, you could for example utilize the event system and let the models that may need to be cleaned attach themselfs as listeners (they should know best whether they may need to be cleaned up), and let a custom error handler dispatch an appropriate event, that way the exception handler doesn't need to know about the app internals.
Here's some very basic, untested example code that should illustrate the idea:
<?php
App::uses('CakeEventManager', 'Event');
class ExampleModel extends AppModel
{
public $name = 'Example';
public function __construct($id = false, $table = null, $ds = null)
{
CakeEventManager::instance()->attach(array($this, 'cleanup'), 'AppErrorHandler.beforeHandleException');
parent::__construct($id, $table, $ds);
}
public function cleanup()
{
// do some magic
}
}
?>
<?php
App::uses('CakeEvent', 'Event');
App::uses('CakeEventManager', 'Event');
class AppErrorHandler extends ErrorHandler
{
public static function handleException(Exception $exception)
{
CakeEventManager::instance()->dispatch(new CakeEvent('AppErrorHandler.beforeHandleException', get_called_class(), array($exception)));
parent::handleException($exception);
}
}
?>
Update
In order to be able to react to specific exceptions only, you could for example utilize the exception class name in the event name, so it would trigger events like ...beforeHandleFooBarException to wich you could explicitly subscribe:
<?php
class AppErrorHandler extends ErrorHandler
{
public static function handleException(Exception $exception)
{
CakeEventManager::instance()->dispatch(new CakeEvent('AppErrorHandler.beforeHandle' . get_class($exception), get_called_class(), array($exception)));
parent::handleException($exception);
}
}
?>
<?php
class ExampleModel extends AppModel
{
public $name = 'Example';
public function __construct($id = false, $table = null, $ds = null)
{
$eventManager = CakeEventManager::instance();
$callback = array($this, 'cleanup');
$eventManager->attach($callback, 'AppErrorHandler.beforeHandleInvalidCallException');
$eventManager->attach($callback, 'AppErrorHandler.beforeHandleIncompleteCallException');
parent::__construct($id, $table, $ds);
}
public function cleanup()
{
// do some magic
}
}
?>
If you would stick with the generic exception event, then another option would be to check the type of the exception in the models event listener callback:
public function __construct($id = false, $table = null, $ds = null)
{
CakeEventManager::instance()->attach(array($this, 'beforeHandleException'), 'AppErrorHandler.beforeHandleException', array('passParams' => true));
parent::__construct($id, $table, $ds);
}
public function beforeHandleException($exception)
{
if($exception instanceof InvalidCallException ||
$exception instanceof IncompleteCallException)
{
$this->cleanup();
}
}
public function cleanup()
{
// do some magic
}

How can I create a dual has_many relationship in Laravel?

I have a database with a Employee table and a Customer table. The Employee table has 2 one_to_many relationships with the Customer table; the foreign keys in the Customer table are 'primary_sales_contact_id' and 'primary_service_contact_id'. Both obviously refer to the id field on the Employee table.
How do I set up a migration for this, and how would I subsequently create a model for it? I'm a newbie in Laravel, so apologies if its blindingly obvious, and thanks for your time.
Employee migration
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEmpoyeeTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('employee', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('employee');
}
}
Customer migration
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCustomerTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('customer', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->integer('primary_sales_contact_id')->unsigned();
$table->integer('primary_service_contact_id')->unsigned();
$table->foreign('primary_sales_contact_id')->references('id')->on('employee');
$table->foreign('primary_service_contact_id')->references('id')->on('employee');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('customer');
}
}
Employee model
class Employee extends Eloquent
{
protected $table = 'employee';
public $timestamps = false;
public function customersService() {
return $this->hasMany('Customer', 'primary_service_contact_id');
}
public function customersSale() {
return $this->hasMany('Customer', 'primary_sales_contact_id');
}
}
Customer model
class Customer extends Eloquent
{
protected $table = 'customer';
public $timestamps = false;
public function primarySalesContact() {
return $this->belongsTo('Employee', 'primary_sales_contact_id');
}
public function primaryServiceContact() {
return $this->belongsTo('Employee', 'primary_service_contact_id');
}
}
All stuff use like:
$customer = Customer::find(1);
echo $customer->primaryServiceContact;
$employee = Employee::find(1);
echo $employee->customersSale;

using soap client in model

I should get the available products list and their prices from another server by WSDL (and NuSOAP).
No views is needed (and no controllers I think); So I create a model with no tables (because I don't want to store server data)
And use App:import('Vendor', 'path_to_nusoap.php') at the beginning of my model file.
Let's see my model:
<?php
App::uses('AppModel', 'Model');
App::import('Vendor', 'nusoap' . DS . 'nusoap.php');
/**
* MyModel Model
*
*/
class MyModel extends AppModel {
public $useTable = false;
public $client = new nusoap_client('url', 'WSDL');
public function products(){
$products = $client->call('getProductsList');
////
return $products;
}
public function prices(){
$prices = $client->call('getPricesList');
////
return $prices;
}
}
but it causes an error (on that line: public $client)
Now, the questions:
How to solve that error? (use a contractor function?)
Am I wrong to use this functions on model? (instead of controller)
Sorry for my terrible English.
Thanks.
you cannot create an object outside of a method scope!
use a constructor:
public $Client;
public function __construct() {
$this->Client = new nusoap_client('url', 'WSDL');
}
public function products() {
$products = $this->Client->call('getProductsList');
return $products;
}

Resources