I'm running with cakephp version 2.0.2 and was scratching my head as to why a form submission that submits data to an association of models was not revealing error messages for the associations.
I've been digging into the Model class to diagnose further. I found that if the primary model for the form had its own validation errors, then no validation errors for any associations would ever be revealed in the returned:
$this->validationErrors
But I think I found the smoking gun. In the Model.php's validateAssociated method, you'll see this:
$this->validationErrors = $validationErrors;
if (isset($validationErrors[$this->alias])) {
$this->validationErrors = $validationErrors[$this->alias];
}
The first line sets $this->validationErrors to contain all built up errors across all associations. But if $validationErrors contains errors for the key of $this->alias which is the primary model name, then as you can see, $this->validationErrors gets overwritten to just those errors.
So this begs the question.... why? I'm so certain this is a bug I want to modify my Model.php and I think it'll work. But I wanted to get this in front of others in case I'm doing something really stupid here.
I had the same issue today. Its like that for BC. I know, it sucks. It should be a bug. The way I work around it is by re-formatting validation errors.
// AppModel.php
public function formatValidationErrors($models) {
foreach($models as $model => $assoc) {
if (is_numeric($model)) {
$model = $assoc;
$assoc = null;
}
$this->validationErrors[$model] = $this->{$model}->validationErrors;
if ($assoc) {
$this->{$model}->formatValidationErrors($assoc);
}
}
}
I call that if validation fails, and pass an array like you would do to contain. You can use that if you don't want to modify the core.
Related
I upgraded my application from Sencha Touch 2.4 to ExtJs 6.5.3.
On Sencha Touch 2.4, there is a function for the Association (Ext.data.association.Association) by the name of getType() that returns the association-type as a string, for example: "hasOne".
as seen for example, in here (the Model source code of Sencha Touch 2.4):
for (i = 0; i < associationCount; i++) {
association = associations[i];
associationName = association.getName();
type = association.getType();
allow = true;
if (associationType) {
allow = type == associationType;
}
if (allow && type.toLowerCase() == 'hasmany') {
In my project, by understanding if the association type is hasmany, hasone or belongsto I can "choose" what type of SQL query to create (not originally written by me, but that's a large project and I can't contact the original developer), so that's a must for me.
I try to look into the Extjs 6 / 6.5 documentation and couldn't find anything.
Seems like it was deprecated a long time ago.
I was thinking about inserting the "type" inside the model as an object of models and types, for example:
{
'MyProject.model.Receipt': 'hasone',
'MyProject.model.Order': 'hasmany',
'MyProject.model.invoice': 'belongsto',
...
}
and then try to access it from the model itself and find the type by the association "parent" model.
It feels like a risk and an overkill for such a (suppose-to-be) easy task.
I tried to also find online for solutions but it looks like no one ever tried to find a solution for something like that.
Thank you
The associations property on a record is a bit tricky and a little obscured from us as developers. If you use the inverse property in your associations, they'll sometimes appear in the associations property, which is problematic. I've raised an issue with Sencha Support asking for a reliable method on returning the actual associations on a record, but I've also come up with an interim solution to determine if they're truly an association. I've extended this idea to maybe something that might help you with the types? But you'll have to test this out to determine if it actually works for you... I only use hasOne and hasMany in my code, so I don't know if this is right for belongsTo.
Here's the Fiddle, and here's the code:
Ext.override(Ext.data.Model, {
isActualAssociation: function (role) {
var isThing = !role.instanceName || role.fromSingle;
if (!isThing) {
console.log('found weirdo', role)
}
return isThing;
},
getAssociationType: function (association) {
if (association.fromSingle) {
return 'hasOne';
}
else if (association.storeName) {
return 'hasMany';
}
return 'belongsTo';
}
});
I've two controllers one is "Upload" which deals with images uploads and other is "Page" whid deals with the creation of pages of CMS now if in my "Upload" controller I load both the models i.e 'image_m' which deals with image upload and "page_m" which deals with the pages creation I've highlighted the relevant code my problem is if I access the variables in the view
$this->data['images'] = $this->image_m->get(); sent by this I can access in foreach loop as "$images->image_title, $images->image_path" etc
But the variable sent by this line ***$this->data['get_with_images'] = $this->page_m->get_no_parents();*** as $get_with_images->page_name, $get_with_images->page_id etc produces given error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: upload/index.php
Line Number: 20
what is the difference between these two access levels one for $image & other for $get_with_images because I can only access its values as $get_with_images
class Upload extends Admin_Controller {
public function __construct() {
parent::__construct();
***$this->load->model('image_m');
$this->load->model('page_m');***
}
public function index($id = NULL) {
//var_dump($this->data['images'] = $this->image_m->get_with_images());
//$this->data['images'] = $this->image_m->get_with_images();
***$this->data['images'] = $this->image_m->get();***
$this->data['subview'] = 'admin/upload/index';
if ($id) {
$this->data['image'] = $this->image_m->get($id);
count($this->data['image']) || $this->data['errors'][] = 'Page Could not be found';
}
$id == NULL || $this->data['image'] = $this->image_m->get($id);
/*this calls the page_m model function to load all the pages from pages table*/
***$this->data['get_with_images'] = $this->page_m->get_no_parents();***
You are not posting all your code so its hard to tell but is it because you used $this-> in the controller, but you haven't done the same thing in the view?
In this case i would recommend not using $this-> because its not necessary. Also its much better to check for errors etc when you call the model so do something like
if ( ! $data['images'] = $this->image_m->get($id) ) {
// Failure -- show an appropriate view for not getting any images
// am showing $data in case you have other values that are getting passed
$this->load->view( 'sadview', $data ); }
else {
// Success -- show a view to display images
$this->load->view( 'awesomeview', $data ); }
so we are saying if nothing came back - the ! is a negative - then show the failure view. Else $data['images'] came back, and it will be passed to the view. note i have not had to use $this-> for anything and it won't be needed in the view.
Would also suggest using separate methods - have one method to show all images and a separate method like returnimage($id) to show an image based on a specific validated $id.
====== Edit
You can access as many models as you want and pass that data to the View. You have a different issue - the problem is that you are waiting until the View to find out - and then it makes it more difficult to figure out what is wrong.
Look at this page and make sure you understand the differences between query results
http://ellislab.com/codeigniter/user-guide/database/results.html
When you have problems like this the first thing to do is make a simple view, and echo out directly from the model method that is giving you problems. Its probably something very simple but you are having to look through so much code that its difficult to discover.
The next thing is that for every method you write, you need to ask yourself 'what if it doesn't return anything?' and then deal with those conditions as part of your code. Always validate any input coming in to your methods (even links) and always have fallbacks for any method connecting to a database.
On your view do a var_dump($get_with_images) The error being given is that you are trying to use/access $get_with_images as an object but it is not an object.
or better yet on your controller do a
echo '<pre>';
var_dump($this->page_m->get_no_parents());
exit();
maybe your model is not returning anything or is returning something but the data is not an object , maybe an array of object that you still need to loop through in some cases.
I have an application in which we give a very friendly interface for managing data. This is done through many controllers' add/edit/view functions. But now the requirement has come that we should have "super admins" able to edit anything, and scaffolding will give them a quick and dirty manner of changing data. Since scaffolding uses add/edit/view by default, I've unintentionally overwritten the ability to scaffold.
I can't just go and change all my calls to edit/add for our "user friendly" data managing. So I want to essentially ignore the add/edit/view when, for example, a user has a flag of "yes, please let me scaffold". I imagined it would be something like:
public function edit($id) {
if (admin_user) {
$scaffold;
} else {
[user-friendly version code]
}
}
But no dice. How can I achieve what I want?
suppose you already have admin users and you want to scaffold only super-user:
Also suppose you store the information about beeing a super-user or not in a column named super in the users table
in your core.php
Configure::write('Routing.prefixes', array('admin', 'super));
in your appController
public $scaffold = 'super';
beforFilter() {
if($this->Auth->user('super') && !isset($this->params['super'])
$this->redirect(array('super' => true));
}
Now I can't try this code but the idea should work.
edit: we need to check if we are already in a super_action to avoid infinite redirect
I follow in book.cake and I don't know I should send something to the parameters.
function beforeSave() {
if (!empty($this->data['Article']['create_dt']) && !empty($this->data['Article']['modified_dt'])) {
$this->data['Article']['create_dt'] = $this->dateFormatBeforeSave($this->data['Article']['create_dt']);
$this->data['Article']['modified_dt'] = $this->dateFormatBeforeSave($this->data['Article']['modified_dt']);
}
return true;
}
I try to search example but don't found.
I need many example
somebody can help me to find big resource
thank for suggest
beforeSave is called automatically by Cake before it saves data. In it, you can do whatever you want to do before each save. Typically this means altering $this->data, which is the data that is about to be saved.
The method is passed one parameter: an array of the form array('validate' => true/false, ('fieldList' => array(...)). This corresponds to the two extra parameters you can supply to save():
$this->Model->save($this->data, false, array('foo', 'bar'));
In this case the array would look like
array('validate' => false, 'fieldList' => array('foo', 'bar')).
You can accept this array by specifying an argument:
public function beforeSave($options) { ... }
$options will look like described above. You can use this information any way you want.
If you don't return true from beforeSave, the save operation will be canceled altogether.
That's all.
try using created and modified magic fields with datetime type in table cake would automatically handle them
i want to mention, that beforeSave() should be used carefully, because it is used on every time when data is saved with this model.
if you forget that it is used, you will get unexpected results.
Happens to me several times... ;)
Little history; I hate the fact that I can't use enums in CakePHP, but I get it. However, another pet peev I have is that my Booleans return 0 or 1 and there is no way to universally turn them to yes' and no's.
So I though I would create a little function in the afterFind method of the AppModel to do this for me. The first step I wanted to take was to identify which columns where boolean (since some columns will return zeros and ones that do not need to be converted). I devised this little peace of code:
function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
foreach($this->_schema as $col => $colDetails){
if($colDetails['type'] == 'boolean')
$this->_booleans[] = $col;
}
}
However a quick debug($this) in the model show that only the current model's boolean columns are captured. When I hit those columns directly the $this->_booleans show up but again, not those of associated models.
I've looked though the manual and the API..I see no mention as to how to approach a solution.
What am I doing wrong?
Enums are not supported by CakePHP in order to make an application database type independent. Enums are not supported by many database engines. The simplest solution for your task is:
echo $model['boolField'] ? 'Yes' : 'No';
The problem is that $this->_booleans in the AppModel only contains the schema details of the current model. In fact, the code is probably working. You should check $this->_booleans and $this->Related->_booleans, and I bet you'll find what you're looking for.