CakePhp find doesn't fetch hasMany relationships - cakephp

I've got these simple models:
// ActivityLog
class ActivityLog extends AppModel {
public $name = 'ActivityLog';
public $useTable = "activity_logs";
public $hasMeny = array("ActivityLogMedia");
}
// ActivityLogMedia
class ActivityLogMedia extends AppModel {
public $name = 'ActivityLogMedia';
public $useTable = "activity_logs_media";
public $belongsTo = array('ActivityLog','Media');
}
// Media
class Media extends AppModel {
public $name = 'Media';
public $useTable = "media";
public $hasMeny = "ActivityLogMedia";
}
when I try to do this find:
$this->ActivityLog->find('all', array(
'conditions' => array('ActivityLog.id' => $ret['ActivityLog']['id']),
'recursive' => 2
));
Cake doesn't return any ActivityLog associated models.
Can anybody give me an explanation and/or a solution?

You have a typo, $hasMeny should be $hasMany.

Related

Laravel ORM relation return data as array instead of object

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');
}
}

Laravel Cart and controller

I am having issue with the route and controller.
The error code consist of sql column not found which is looking for column id from items table. Which im quite curious due to the differences with my migration.
CartController.php
namespace App\Http\Controllers;
use App\Cart;
use App\CartItem;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CartController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function addItem ($itemNo){
$cart = Cart::where('user_id',Auth::user()->id)->first();
if(!$cart){
$cart = new Cart();
$cart->user_id=Auth::user()->id;
$cart->save();
}
$cartItem = new Cartitem();
$cartItem->itemNo=$itemNo;
$cartItem->cart_id= $cart->id;
$cartItem->save();
return redirect('/cart');
}
public function showCart(){
$cart = Cart::where('user_id',Auth::user()->id)->first();
if(!$cart){
$cart = new Cart();
$cart->user_id=Auth::user()->id;
$cart->save();
}
$items = $cart->cartItems;
$total=0;
foreach($items as $item){
$total+=$item->product->price;
}
return view('cart.view',['items'=>$items,'total'=>$total]);
}
public function removeItem($id){
CartItem::destroy($id);
return redirect('/cart');
}
}
ItemController.php
<?php
namespace App\Http\Controllers;
use App\Item;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
class ItemController extends Controller
{
public function index(){
$items = item::all();
return view('admin.items',['items' => $items]);
}
public function destroy($itemNo){
item::destroy($itemNo);
return redirect('/admin/items');
}
public function newItem(){
return view('admin.new');
}
public function add() {
$file = Request::file('file');
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put($file->getFilename().'.'.$extension, File::get($file));
$entry = new \App\File();
$entry->mime = $file->getClientMimeType();
$entry->original_filename = $file->getClientOriginalName();
$entry->filename = $file->getFilename().'.'.$extension;
$entry->save();
$Item = new Item();
$Item->file_id=$entry->id;
$Item->itemName =Request::input('name');
$Item->itemDescription =Request::input('description');
$Item->price =Request::input('price');
$Item->imageurl =Request::input('imageurl');
$Item->save();
return redirect('admin/items');
}
}

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: issues with different model and used table names

I'm having a problem where I get an error saying,
Error: Database table catalog_product_entity for model CatalogProductEntity was not found.
I want the model MagentoCatalogProductEntity to use the table catalog_product_entity, but why is it looking for the model named after the table I defined, instead of the actual model name, MagentoCatalogProductEntity?
I have...
magento_catalog_product_entity.php
class MagentoCatalogProductEntity extends AppModel {
public $name = 'MagentoCatalogProductEntity';
public $useTable = 'catalog_product_entity';
...
}
magento_catalog_product_entity_controller.php
class MagentoCatalogProductEntityController extends AppController {
public $name = 'MagentoCatalogProductEntity';
....
}
Note that this works, could it be some bug where if the model name and $useTable share similar names, it doesn't work properly?
test.php
class Test extends AppModel {
public $name = 'Test';
public $useTable = 'catalog_product_entity';
...
}
EDIT 2013-06-12
I couldn't figure out what was wrong with the setup I provided above. I am currently just using the following.. I really want that "magento" prefix in the file and class names, but this will do for now.
catalog_product_entity.php
class CatalogProductEntity extends AppModel {
public $name = 'CatalogProductEntity';
public $useTable = 'catalog_product_entity';
...
}
catalog_product_entity_controller.php
class CatalogProductEntityController extends AppController {
public $name = 'CatalogProductEntity';
....
}

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