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');
}
}
Related
I am trying to insert a new record in my SQL Server database, but it won't insert because the identity is set as 0, which will lead to the duplication exception.
I've created my table id as follow:
id BIGINT NOT NULL PRIMARY KEY IDENTITY(1,1)
Here is my model:
class MyTable extends Model
{
const CREATED_AT = 'createdOn';
const UPDATED_AT = 'modifiedOn';
protected $table = 'my_table';
protected $primaryKey = 'id';
protected $fillable = [
...
];
protected $guarded = [];
protected $attributes = [
'id' => false,
...
];
protected $appends = [
...
];
protected $dateFormat = 'Y-m-d H:i:s';
public $validations = [
...
];
public $validationMessages = [
...
];
}
and the model observer:
class MyTableObserver
{
protected $dbDefaultConnection;
public function __construct()
{
$this->dbDefaultConnection = DB::getDefaultConnection();
}
public function creating(MyTable $model)
{
if ($this->dbDefaultConnection == "sqlsrv") {
DB::unprepared('SET IDENTITY_INSERT ' . $model->getTable() . ' ON');
}
}
public function created(MyTable $model)
{
if ($this->dbDefaultConnection == "sqlsrv") {
DB::unprepared('SET IDENTITY_INSERT ' . $model->getTable() . ' OFF');
}
}
}
I was trying to get the relational model data like
{{$OrderInfo->CustomerInfo->Phone}}
but it's giving error like
Trying to get property of non-object
While we can easily access the returned data like
{{$OrderInfo->CustomerInfo['Phone']}}
My work is temporarily working, but I was not satisfied. It should worked to access the data as an object. Because, I think that is right process to access the data. Please can anyone help me to come out from the problem.
Thanks so much in advance for your valuable time!
Order, Customer Model & My Controller Code given below
use Carbon\Carbon;
use App\OrderInfo;
use App\CustomerInfo;
use Mail;
class AdminOrderController extends Controller
{
public function index(Request $request)
{
$Orders = OrderInfo::orderBy('OrderDate', 'DESC')->get();
return view('admin.admin-order-list', [
'Orders' => $Orders,
]);
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderInfo extends Model
{
protected $table = 'order_info';
public $timestamps = false;
protected $primaryKey = 'OrderId';
public function CustomerInfo()
{
return $this->belongsTo('App\CustomerInfo', 'CustomerID');
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class CustomerInfo extends Model
{
protected $table = 'customer_info';
public $timestamps = false;
protected $primaryKey = 'CustomerID';
protected $fillable = ['CustomerID','Phone'];
public function OrderInfo()
{
return $this->belongsTo('App\OrderInfo', 'CustomerID');
}
}
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
I've the following services:
public with sharing class LibraryService {
public static void remove(String jsonString) {
Library__c library = [ SELECT Id, ilms__Library_Name__c FROM ilms__Library__c WHERE Id = libraryId ] ;
AccessService.deleteReviewerGroup(library);
delete library;
}
}
AccessService class
public with sharing class AccessService {
public static void deleteLibraryReviewerGroup(Library__c library) {
List<Library__Share> reviewersGroups = [ SELECT UserOrGroupId FROM ilms__Library__Share WHERE AccessLevel = 'Read' AND ParentId = :library.Id ];
System.debug('reviewersGroups: ' + reviewersGroups);
if(reviewersGroups.size() == 1) {
String reviewersGroupId = reviewersGroups[0].UserOrGroupId;
delete reviewersGroups;
AccessService.deleteReviewerGroup(reviewersGroupId);
}
return;
}
#future
public static void deleteReviewerGroup(String groupId) {
List<Group> reviewerGroup = [ SELECT Id FROM Group WHERE Id = :groupId ];
delete reviewerGroup;
}
}
Now, when I try to test the LibraryService remove method, I keep receiving the below error:
first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa).
#isTest(SeeAllData=true)
private class TestLibrary {
static testMethod void testRemoveLibrary() {
Library__c library = new Library__c(...);
Boolean isRemoved = LibraryService.remove(TestUtilsClass.idJson(library.Id));
System.assertEquals(isRemoved, true);
}
}
I tried adding Test.startTest() and Test.stopTest() to the testRemoveLibrary method, but I still get the same error. Am I doing something wrong? How do I fix this?
#isTest(SeeAllData=true)
private class TestLibrary {
static testMethod void testRemoveLibrary() {
Library__c library = new Library__c(...);
Test.start();
Boolean isRemoved = LibraryService.remove(TestUtilsClass.idJson(library.Id));
Test.stop();
System.assertEquals(isRemoved, true);
}
}
Please add Test.start and stop including your method.
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;
}