associated data & displayField cakephp - cakephp

I have shipments, orders and users. When I try and set up a new shipment, the Order displayField is showing up correctly as the label in my select dropdown. The User though is only showing the actual id of the user instead of the displayField.
What am I doing wrong? I'm using admin scaffolding. Is that the problem?
class Shipment extends ShipmentsAppModel {
public $belongsTo = array('Order', 'User');
}
class Order extends AppModel {
public $belongsTo = array('User');
public $name = 'Order';
public $displayField = 'title';
}
class User extends UsersAppModel {
public $displayField = 'email';
}

Seeing, UsersAppModel extends by User. It should AppModel according to CakePHP convention. Order is Extending AppModel and its working as u expect.
Try to use AppModel instead of UsersAppModel. The $displayField inherits from AppModel automatically, its a magic variable, defined in CakePHP core with default id field. Re-using that variable that variable in our model we just re-write that value and here AppModel is the linker.
But, here I think UsersAppModel has no relation with CakePHP core model inheritance like AppModel and that is the point of issue.
So instead of
class User extends UsersAppModel {
public $displayField = 'email';
}
use
class User extends AppModel {
public $displayField = 'email';
}

First check your code syntax
it should be
class Shipment extends AppModel {}
and
class User extends AppModel{}

Related

React login screen with laravel [duplicate]

In my LoginController under Auth, I have used the following codes:
namespace App\Http\Controllers\Auth;
use App\Model\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Hash;
use Auth;
use DB;
use App\Model\UserAdmin;
class LoginController extends Controller {
use AuthenticatesUsers;
public function __construct() {
$this->middleware('guest')->except('logout');
}
public function doLogin(Request $request) {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'status' => '1',
);
if (Auth::guard('admin')->attempt($userdata)) {
return Redirect::intended('/administrator/dashboard')->with('successMessage', 'You have successfully logged in.');
}
}
}
And in UserAdmin (model) under app/Model is as follows:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Config;
class UserAdmin extends Authenticatable {
protected $table = 'adminusers';
public $timestamps = false;
protected $fillable = ['firstName', 'lastName', 'email', 'company', 'website'];
public function __construct() {
parent::__construct(); // Don't forget this, you'll never know what's being done in the constructor of the parent class you extended
}
}
After submitting the login details, it shows me the error:
Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Model\UserAdmin given, called in /var/www/html/XXXXXX/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 379
I suppose that you required to add implements \Illuminate\Contracts\Auth\Authenticatable to your UserAdmin model class definition.
class UserAdmin extends Model implements
\Illuminate\Contracts\Auth\Authenticatable
You must use Authenticatable in User model
for example:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
//your code
}
You must declared use AuthenticableTrait for Authenticatable interface.
For example :
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;
class Company extends Model implements Authenticatable
{
use AuthenticableTrait;
Try and run 'composer dump-autoload' to check for "ambiguous User class resolution". It is likely you have two classes Defined as User Class.
use Illuminate\Foundation\Auth\AuthenticatesUsers;
Then in your model class, extends AuthenticatesUsers instead of Model.
You must extends Authenticatable class and implements JWTSubject in User model
For example :
class User extends Authenticatable implements JWTSubject {
Go to your Model and instead of extending Model, extend User
<?php
namespace App;
class Staff extends \Illuminate\Foundation\Auth\User
{
}

cakePHP and conditions on deep association

The data structure is:
tenants {id, ...}
contracts {id, active, tenant_id, ...}
debts {id, contract_id, ...}
the desired data is:
tenants list, filtered by contracts and debts.
conditions:
contract must be active.
tenant has debts
keep in mind:
tenant has many contracts
contracts has many debts
contain won't help, since it will not filter the tenants.
My idea is to manually create the joins, and add filters to them. but how exactly?
my questions:
How can i do it with native cake?
how would you do it?
You have to put all this logic on models.
For each tables create a model (You can use the Cake Bake tools or write it by yourself).
class Tenant extends AppModel{
public $hasMany = array('Contract'); // feel free to ajust settings
}
class Contract extends AppModel{
public $belongsTo = array('Tenant');
public $hasMany = array('Debt');
}
class Debt extends AppModel{
public $belongsTo = array('Contract');
}
Now you can use on your Tenant controller something like that :
function action(){
$this->Tenant->recursive = 2;
$all_contracts = $this->Tenant->find('all');
}
But that solution is not the best, you can wrap this logic into model's method.
class Tenant extends AppModel
{
public $hasMany = array('Contract');
public function contracts($is_active = true) {
/*
Create your own logic here, you have many solutions to retrieve data like
load the containable behaviors, create a custom find method, custom query ...
Do stuff here not in controller action.
*/
$this->Behaviors->load('Containable');
$this->contain(array(
'Contract' => array(
'conditions' => array(
'active' => $is_active
),
'Debt'
)
));
return $this->find('all');
}
}
class Contract extends AppModel{
public $belongsTo = array('Tenant');
public $hasMany = array('Debt');
}
class Debt extends AppModel{
public $belongsTo = array('Contract');
}
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types

Find() on a non-object?

I am trying to access the find() on another model in a controller. I am getting a
Call to a member function find() on a non-object error
PollsController.php (controller):
class PollsController extends AppController{
public function add(){
$this->set('appNames', $this->App->find('list'));
}
}
Poll.php (model):
class Poll extends AppModel{
public $belongsTo = array ('App');
}
App.php (model):
class App extends AppModel{
public $hasMany = array('Poll');
}
Note: I have also tried $this->Poll->App->find('list') and still get the same error.
App is reserved keyword. When I change the class to SomethingController.php and Something.php, everything works again.
If you want access another model functions you need load model. There is few ways to do it.
This one is only useful for controllers
$this->loadModel('App');
$this->App->find('first');
This one is good for models and controllers
Read more: CakeAPI: Class ClassRegistry
$appModel = ClassRegistry::init('App');
$appModel->find('first');
This one is good for controllers
Read more: CakeAPI: $uses variable in controller
//in Post Controller
public $use=array('Post','App');
Please load modal in your controller
public $uses = array("Poll");

CakePHP: Connection between model and controller failed

I've got a problem overhere with my newest CakePHP application.
I've made a model and controller for Categories.
Model: Categorie.php
class Categorie extends AppModel{
public $name = 'Categorie';
}
Controller: CategoriesController.php
class CategoriesController extends AppController {
public function index(){
$this->set('list', $this->Categorie->find('all'));
}
}
But when I visit the page I get the error:
Fatal error: Call to a member function find() on a non-object
When I add
$this->loadModel('Categorie');
to the index-function in my controller it will work.
So I guess the connection between the model and controller is not working but I'm not sure and I don't know how to fix this.
Please help me out.
thnx
CakePHP uses the controller names singular variant for matching the model, and the singular of categories is category, not categorie.
As #ndm said, you should use CakePHP conventions.
Your table should be called categories, then your model should look like this:
class Category extends AppModel{
}
And then you can include it with:
$this->loadModel('Category');
On CategoriesController you can use it like this:
class CategoriesController extends AppController {
public function index(){
$this->set('list', $this->Category->find('all'));
}
}
If you want to name your table otherwise, you can make use of $useTable variable in controller to specify the name of the table the model will work with.
You could also make use of the controller's $uses property:
public $uses = array('Categorie');

How to name a controller file for uppercase table in cakephp

Hye. I want to make a controller and model for a table which is named in uppercase and abbreviation.
For example, the table name is PS_DEPT_TBL
1) What would be the controller file name? Is it PSDEPTTBL_controller.php? The code below seems to be not working for the controller.
class PSDEPTTBLController extends AppController {
var $uses = 'PS_DEPT_TBL';
var $scaffold ;
}
2) I name the model file as PSDEPTTBL.php and code it as below.
class PSDEPTTBL extends AppModel {
var $useTable = 'PS_DEPT_TBL';
}
But the error shows that there isn't any controller of the table. I'm new to cakephp. Help me.
There is no need for your controller to be called the same as your table. The same goes for your model.
How about something this:
// ThingsController.php
class ThingsController extends AppController
{
var $uses = array('Thing');
}
// Thing.php
class Thing extends AppModel
{
var $useTable = 'PS_DEPT_TBL';
}
// Config/routes.php
Router::connect(
'/PSDEPTTBL/:action/*', array('controller' => 'things')
);
The key idea here is to make your code readable. If you have a legacy database, the best thing you can do for yourself is to hide it all in all the models.

Resources