How Do I Filters the list by refModel('Model_user') in the Models? - atk4

I am unable to find how to filter the drown down which i got by using refmodel in my model which extends model_table
thanks in advance

For any view in toolkit you can use setController and getController. refModel fields from Model will set a proper controller for a form field. It uses type reference.
Once form in initialized, you can interact with controller of a field by
$field->getField('myfield')->getController()->addCondition('type','admin');
Alternatively the following might work too:
$field->getField('myfield')->dictionary()->addCondition('type','admin');

Related

CakePHP How to accomplish a NOT IN

I want to accomplish a NOT IN clause in CakePHP, but I am not able to achieve it. I've been reading the CakePHP Cookbook and some answers here in StackOverflow but it's not working for me.
I have a Table named 'Hotel' with all its model, controller and views. In the view template I built a CakePHP Cell where I want to show other Hotels, except for the hotel that is currently being viewed. If for example, I am showing the view of a hotel with id #5, I want to show other hotels options, except for that hotel view id number.
I have the following query in the Cell Controller:
$hotels = $this->Hotels->find('all')
->where(['Hotels.id NOT IN' => $current_hotel_id])
->limit(4)
->order('rand()')
->toArray();
I want $current_hotel_id to have the value of current hotel that it's being viewed. That's the approach I am taking.
Any useful information would be appreciated.
If you need some value that is available in your controller in cell, you must pass that value to it.
First, declare an argument in cell action:
class HotelsCell extends Cell{
public function hotels($current_hotel_id){
//your code here
}
}
Then, in your view, pass argument to cell:
$this->cell("Hotels::hotels",[$hotel->id])
More info in docs: Passing Arguments to a Cell

Manipulating convention of cakePHP

i have a controller PostmustController which doesn't have any models naming Postmust. it will load a model named Post how can i do it in cakePHP.
Learned and solved it. And there is a anotherway.
You can also use this
Controller::loadModel('Post');
You can learn the detail from the following link.
Uses following array in your controller it will automatically load models in your controller.
public $uses = array('Post');

Backbone view based on model from collection, how to update view?

I am looking for a bit of direction I am still fairly new to Backbone and am currently creating a test application to learn more.
My problem is this, I am populating a backbone view with a underscore template. I load a collection of models, then I find the model I need and populate these values into the template. There can be many pages based on the template so I have a dynamic route that accepts an id.
My problem is I want to add a next feature, that would change the current page and reload the template with the new model.
I have tried a crude method along the lines of :
Backbone.history.navigate(newLocation)
However this did'nt work, please note newLocation is actually populated with the route and the id I want to navigate to.
I will add some code from my view below, I won't include the full code however if it is needed please ask.
Any help or a push in the right direction would be great.
Thanks in advance
You need to use your router object's navigate method rather than than history's class method, and you need to pass it the option `{trigger: true} in order to invoke the corresponding route function.

cakephp model: how to display a message in model function

The following function in MerryParent model returns $merry_parent_id or empty string if it is not able to find any. If it is going to return an empty string, i want to stop that and display an error message in the model itself instead of creating an if then else stmt in controller and displaying the error message there. How can I do that?
I don't know on how to display error msgs in model function. In controller I know that I can use $this->Session->setFlash('my error msg'). But that doesn't work here.
By the way, i'm trying to abide by 'fat model thin controller approach'. :)
class MerryParent extends AppModel{
//relationships are displayed here
//form field validations are displayed here
function getMerryParentId($email){
$merry_parent_id=$this->field('id',array('MerryParent.email'=>$email));
return $merry_parent_id;
/*instead as return $merry_parent_id, I want
if ($merry_parent_id!='')
return $merry_parent_id;
else
//display error message here.
}
}
thank you.
You shouldn't be displaying errors via the model. If you want to display something just for testing purposes, you can debug() it in the model...etc, but in general, you should use the normal MVC structure, and use the model to retrieve the data, use the controller to process it, and the view to display it.
The "Fat model / Skinny controller" thing is great to follow as a guideline, but when you follow it too far and stop following the more-important MVC structure, it's not a good thing. It's not meant to be "Fat model / Empty controller". :)

How can I get record ID on Edit view layout?

How can I get ID of current record, on Edit view layout?
You can load the Model in question and access it that way from within the view (which, in my opinion, is an awful idea) or you can 'set' it within your controller action:
// In the controller action that renders the view
$this->set('current_id',$this->ModelName->id);
// Access it this way in the view/layout:
<?php echo $current_id; ?>
You could also grab the current record ID using ajax, but that doesn't sound like something that would benefit you here.
Good luck
Use $this->data['ModelName']['id'] in the view
Since 2.3, as long as you have $this->Form->hidden('fieldname'); you can reliably use $this->Form->value('ModelName.fieldname') for the value of a field in the form. When the view is rendered, just as html input tags get a value based on the model used in the controller and referenced in the $this->Form-create('ModelName'), when rendering the $this->Form->value('ModelName.fieldname') has a value.
In the controller, $this->request->data['ModelName'] is set to an array of the Model's data in CakePhp, where ['id'] points to the value of the id field.
$this->Form->create('ModelName');
$this->Form->input('id');
$this->Form->end();
Corresponds to controller logic where $this->request->data['ModelName'] gets an associative array with at least "id" => some id value depending on the data read from the database.
Note that when you use $this->Form->value() to retrieve form data, it will work when the screen is initially rendered by the controller, but may fail when the screen is rendered again because a model validation rule that used a function in the model definition failed.
To make sure that the $this->Form->value() is reliable with more rigorous model validation logic, use $this->Form->hidden('fieldName'); to hold the data in $this->request->data['ModelName'] throughout multiple model validation iterations.

Resources