How to access cakephp model's validate from custom view template - cakephp

I have created a 'form' template to bake custom theme views (Cake 2.6.0). I am trying to access field properties from the Model's $validate array. However, accessing $model->validate shows an empty array. My model has several fields with rules defined in it's $validate property.
Is the $validate property not accessible while baking custom views? If not, how do I find out whether a field is required, or if it uses 'rule =>' 'url', for example?

The view template(s) used by cake bake view are an instance of class TemplateTask and does not have direct access to the Model, View or Controller. What you want to do is import the controller to your custom view template:
Console\Templates\[themename]\views\[template].ctp
<?php
// The Controller's name
$controllerName = Inflector::pluralize($modelClass).'Controller';
// Import the Controller
App::import('Controller', $controllerName);
// Instantiate the Controller
$Controller = new $controllerName();
// Load the Controller's classes
$Controller->constructClasses();
//...the rest of your template
You now have access to your controller # $Controller. To access your validate property, you would use $Controller->{$modelClass}->validate.

Related

How to add queries in model in Cakephp 2.x and call that function in controller

I want to add my save, update, find queries in model and call those functions in controller, rather than adding the queries in controller.
I'm not sure what's you precisely want to acheive, but I first of all hope you know that CakePHP has implemented save, update and find functions for your models. If not, look at the docs, e.g. here: http://book.cakephp.org/2.0/en/models/saving-your-data.html
If you want to add functions with custom queries to your model, you can simply add them as a normal function in your model, e.g.:
MODEL
public function myMethod(){
// Fetch data
return $this->query("SELECT * FROM pictures LIMIT 2;");
}
CONTROLLER
public function index(){
// Get data from model-method
$data = $this->ModelName->myMethod();
// Send data to view
$this->set(compact('data'));
}
In your model classes you don't have to refer to your model in your code:
In your controller: $this->ModelName->function()
In your model: $this->function()
Has the same meaning.

How to set up datatables for dynamic tables

I have a dynamic table name and tried for datatables in Cake PHP controllers. But it didn't worked.
<?php
$data = $this -> DataTable -> getResponse(Model,action);
?>
Here I do not have any specified model name as I am accessing table model via ClassRegistry::init() in controller . I do not have exact Model name here. What should I then use here? Also what action should I specify to get the data?
You can use
$this->controller->loadModel($Model);
$this->controller->{$Model}->{user any model action}
{user any model action} : you have to make action which has common name for all Model.

Backbone model singleton

I am new to Backbone.js and I have the following problem: I have multiple views that uses the same model. I do not want to re-fetch model on every view render, but I want to fetch it only once and then when view is rendered use that instance/data.
My example: I have 3 views for user. One is some user statistics, another user info and third user profile. After login user lands on user profile view and there I fetch the user model but how could I then pass this model reference around or even better how could I access that model data from different views?
I hope I am not doing any anti-pattern here. I have seen a lot of examples with binding events to model change and then rerender all the views but that is not my case. I am using backbone.js with require.js and underscore template engine.
Just return the instantiated model:
define(function (require) {
var MyModel = Backbone.Model.extend({})
return new MyModel()
});

cakephp calling controller action from model

I have a shopping cart site and on successful purchase I need to send a mail with all product details in the order as attachment. I have used fat model skinny controller approach and all my functions are in model. I have a controller action which will give the order details by passing order id along with view. Using dompdf I can convert this html to pdf and can create a file. So for creating attachment I can use the same function by passing some parameter. My mail sending code is in model. From here I need to call the controller action and need to get the pdf file name that just created. I know calling controller action from model is against MVC architecture. But how can I achieve this functionality ?
'fat' models is a good thing to do, however, try not to put things in a Model that should not be in a Model. In MVC, Models should handle all things related to data.
Fat models
The 'fat' Model concept is to reduce the amount of code in your Controller, by moving data related code to the Model. for example:
In stead of this; (in your Controller):
public function view($id)
{
$this->request->data = $this->SomeModel->find('first', array(
'fields' => array(
// list of fields to retrieve
),
'conditions' => array(
// conditions
),
// etc.
);
}
Move the find instructions to a method inside your model and use this:
public function view($id)
{
$this->request->data = $this->SomeModel->someMethod($id);
}
Other locations to put your code
Code that is not related to data, can also be moved outside your Controller (to make it 'skinny'). CakePHP offers other locations to move your code to, for example inside a Component
Then inside your Controller;
this:
public function view($id)
{
$this->request->data = $this->SomeModel->someMethod($id);
// use functionality of a component
$this->SomeComponent->doSomething();
}
Triggering functionality via Events
To keep code and logic outside your controller, CakePHP 2.x now offers an 'Event' system. This allows you to execute code if a certain event happens. You can pass additional information through the events (the event will become a 'communication channel' that passes through your application).
Sending e-mails for certain events is a good example. The CakePHP also uses sending mails to illustrate the Event system in CakePHP; this is probably what you are looking for:
Dispatching Events - send emails
Make your model method return the data so you have it in the controller and pass it to the other model function together with your pdf related data.

Cakephp: Routing and default views

I'm currently trying to add an admin mode to my cakephp site.
I followed a tutorial on the web and added a routing prefix:
Configure::write('Routing.prefixes', array('admin'));
Then I implemented login and logout functionality.
I added a admin_view.ctp and admin_index.ctp to a model where I want to restrict access. Therefore I deleted view.ctp and index.ctp and expected that only admins could view the model by using:
http://xxxx/model/admin/index
But when I entered
http://xxxx/model/index
a default view appeared that I could not disable (it allows model manipulation). Is there a standard way to disable all these default views or do I have to create a index.ctp and view.ctp that simply show error messages?
If you want to disallow certain action, you need to setup ACL, which is described here. And for authentication, in your controller you need something like this:
class SomethingsController extends AppController
{
var $components = array('Auth');
//this will allow all users access to the index
//and view methods ( but not any other)
function beforeFilter() {
$this->Auth->allow('index','view');
}
// other actions
}
Hope this helps.
Delete default controller methods: function index(), view(), add(), edit() and delete(). Removing *.ctp templates is not enough.

Resources