Cakephp How to Make Request data contain only the selected field values - ajax

Iam Using Cakephp 2.4.6 version. Currently I am facing a problem with some fields in the forms. I have a group of horizontal controls in which the first control is a checkbox which hold the id value.if id is selected then only i want to get all other controls in that row.
is there any way other than getting all values into ajax and send thorough ajax.
TFmPlanProgram' => array(
(int) 0 => array(
't_fm_program_id' => '42',
'number_of_time' => '10'
),
(int) 1 => array(
't_fm_program_id' => '43',
'number_of_time' => '10'
),
(int) 2 => array(
't_fm_program_id' => '44',
'number_of_time' => '15'
),
(int) 3 => array(
't_fm_program_id' => '0',
'number_of_time' => ''
),
(int) 4 => array(
't_fm_program_id' => '0',
'number_of_time' => ''
),
This is my array. in that you can see that 3 and 4 having no id value. but it is passed to the server. i want to pass only the selected ID's.

You can try this before calling $this->TFmPlanProgram->save();
foreach($this->request->data['TFmPlanProgram'] as $key => $value){
if(empty($value['number_of_time'])){
unset($this->request->data['TFmPlanProgram'][$key]);
}
}

Related

Sort by custom field (acf) date picker and publish date

I would like to sort posts using firstly the custom field date picker by ACF plugin for wordpress, and then when that's non existing, by the published date.
Our theme works with cases so I cooked up something like this but I failed misearbly, any suggestions ?
case 'concertdatum':
$wp_query_args['meta_query'] = array(
'relation' => 'AND',
'datum_clause' => array(
'meta_key' => 'datum',
'meta_type' => 'DATETIME'
),
'publish_clause' => array(
'column' => 'post_date_gmt',
'compare' => 'EXISTS',
),
)
$wp_query_args['order'] = array(
'datum_clause' => 'DESC',
'publish_clause' => 'DESC'
);
break;

how to get multidimantional array in laravel

array(
'id' => 1,
'title' =>'BlackBerry Leather Smart Flip Case for BlackBerry PRIV',
'product_type' => 'Mobile',
'product_image' => array(
0 => array(
'product_id' => 1,
'image_src'=> 'https://22198_grande.jpg',
'variant_ids' => 11
)
),
'product_variant' => array(
0 => array(
'product_id' => 1,
'image_id' => 21,
'title' => "BlackBerry Leather Smart Flip Case for BlackBerry PRIV/black",
'price' => 250
)
)
);
I want to change this array to associative array format
id=>1;
title=>BlackBerry Leather Smart Flip Case for BlackBerry PRIV;
product_type=>Mobile;
product_id=>1;
image_src=>https://22198_grande.jpg;
variant_ids=>11;
product_id=>1;
image_id=>21;
title=>BlackBerry Leather Smart Flip Case for BlackBerry PRIV/black;
price=>250
Use laravel map function for this
$multiplied = $schoolkids->map(function ($kids) {
$test = $kids;
$temp = collect($kids->toArray());
return $temp->except(['id','class_id','org_id','section_id','parent_id', 'parent_info', 'class_name', 'classes', 'org_detail'])->merge($kids->orgDetail->toArray())->merge($kids->parentInfo->toArray())->merge($kids->classes->toArray());
});
$multiplied is the main query Kids is the first array function and if you want to remove some ids from array than use except function

Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

In database seeder, I just want to insert some hard-coded data to the table.
$Pro1_id = DB::table('projects')->select('id')->where('projectName', '=', 'Project A')->get();
$data1_1 = array(
'id' => 1,
'projectID' => $Pro1_id,
'attributeID' => 1,
'levelID' => 2,
'percentage' => 20,
'risk_value' => 25186.86311,
'expectation_value' => 706455.9401,
);
$data1_2 = array(
'projectID' => $Pro1_id,
'attributeID' => 2,
'levelID' => 1,
'percentage' => 60,
'risk_value' => 530351.3397,
'expectation_value' => 392207.1248,
);
$data1 = [$data1_1, $data1_2];
DB::table('Mapping')->insert($data1);
However, I got the error:
[ErrorException] preg_replace(): Parameter mismatch, pattern is a
string while replacement is an array
It is so weird, because I did the same to another table, it worked.
DB::table('projects')->insert(array(
array(
'id' => Webpatser\Uuid\Uuid::generate(),
'projectName' => 'Project A',
'creator_id' => $pro1_creatorID,
'create_at' => \Carbon\Carbon::now()->toDateString(),
'lastEditor_id' => $pro1_creatorID,
'edit_at' => \Carbon\Carbon::now()->toDateString(),
'utility' => 1.597119661,
'exponential' => 4.94,
'projectValue' => 1225090.39
),
array(
'id' => Webpatser\Uuid\Uuid::generate(),
'projectName' => 'Project B',
'creator_id' => $pro2_creatorID,
'create_at' => \Carbon\Carbon::create(2014, 12, 12)->toDateString(),
'lastEditor_id' => $pro2_creatorID,
'edit_at' => \Carbon\Carbon::create(2014, 12, 12)->toDateString(),
'utility' => 1.754989409,
'exponential' => 5.78,
'projectValue' => 293760.36
),
array(
'id' => Webpatser\Uuid\Uuid::generate(),
'projectName' => 'Project C',
'creator_id' => $pro3_creatorID,
'create_at' => \Carbon\Carbon::create(2013, 10, 21)->toDateString(),
'lastEditor_id' => $pro3_creatorID,
'edit_at' => \Carbon\Carbon::create(2013, 10, 21)->toDateString(),
'utility' => 1.423114267,
'exponential' => 4.15,
'projectValue' => 1461924.67
)
)
);
I really don't understand why inserting into projects table works, but the one of the mapping table does NOT work.
They are exactly the same method.
I think your code is correct but when you insert the id in array, you are doing the wrong way.
$Pro1_id = DB::table('projects')->select('id')->where('projectName', '=', 'Project A')->get();
Here, $Pro1_id is Collection that contain value return from your query. Sometimes it might be one, but sometimes it might be 2 or 3.... So , your are doing the wrong way when you are inserting the id in the array. So , use foreach loop like this :
foreach($Pro1_id as $pro){
DB::table('Mapping')->insert(array(
'id' => 1,
'projectID' => $pro->id,
'attributeID' => 1,
'levelID' => 2,
'percentage' => 20,
'risk_value' => 25186.86311,
'expectation_value' => 706455.9401,
));
}
For simple , get returns Collection and is rather supposed to fetch multiple rows.
For more info . Check this

How do I create a configurable product with associated products?

The following code does create a configurable product, however, when I open the product in the backend, the following message appears:
Select Configurable Attributes
"Only attributes with scope "Global", input type "Dropdown" and Use To Create Configurable Product "Yes" are available."
A single checkbox is displayed ("Colour Group"), which must be selected before continuing.
When I click "Continue", all of the product data is there as expected EXCEPT for the associated products.
//Mage Product
$mpr = Mage::getModel('catalog/product');
$mpr
->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
->setTaxClassId(5)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setWebsiteIds(array(1))
->setAttributeSetId(4) // You can determine this another way if you need to.
->setSku("C12345")
->setName("C12345")
->setQty(25)
->setShortDescription('short description')
->setDescription('description')
->setPrice(1)
->setStockData(array(
'use_config_manage_stock' => 1,
'is_in_stock' => 1,
'is_salable' => 1,
));
$productData = array(
'7039604' =>
array('0' => array('attribute_id' => '85', 'label' => 'ROYAL','value_index' => '28563', 'is_percent' => 0, 'pricing_value' => '')
,'1' => array('attribute_id' => '192', 'label' => '14', 'value_index' => '28728', 'is_percent' => 0, 'pricing_value' => '')
)
);
$attributeData = array(
'0' => array(
'id' => NULL
,'label' => 'Color'
,'position' => NULL
,'values' => array(
'0' => array('value_index' => 28563, 'label' => 'ROYAL', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '85')
)
,'attribute_id' => 85
,'attribute_code' => 'color'
,'frontend_label' => 'Color'
,'html_id' => 'config_super_product__attribute_0')
,'1' => array(
'id' => NULL
,'label' => 'Rivers Size'
,'position' => NULL
,'values' => array(
'0' => array('value_index' => 28728, 'label' => '14', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '192')
)
,'attribute_id' => 192
,'attribute_code' => 'rivers_size'
,'frontend_label' => 'Rivers Size'
,'html_id' => 'config_super_product__attribute_1')
);
$mpr->setConfigurableProductsData($productData);
$mpr->setConfigurableAttributesData($attributeData);
$mpr->setCanSaveConfigurableAttributes(true);
$mpr->save();
Add this code before $mpr->save();
$SKU = "any-simple product sku enter here";
$productid = Mage::getModel('catalog/product')
->getIdBySku(trim($SKU));
$mpr->assignProduct($productid);
And set simple product sku in $SKU variable. and i have check that when i select global variable then after i see associated product in configure product.
Its work fine !!!
If you are getting redirected to select attribute page, this means that attribute data you set in this sample is not saved correctly.
Try viewing catalog_product_super_attribute after script run (new rows should be added).

Setting Validation Rules at Runtime in CakePHP

My problem right now is that a Model has a set of Validation rules like so:
var $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'uri' => array(
'slugged' => array(
'rule' => '/^[a-z0-9-_]+$/i',
'message' => 'This field should only contain characters, numbers, dashes and underscores'
),
'uniqueUrl' => array(
'rule' => array('uniqueUrl'),
'message' => 'A page has already acquired this url'
)
),
'meta_keywords' => array(
'rule' => 'notEmpty'
),
'meta_description' => array(
'rule' => 'notEmpty'
),
'layout' => array(
'rule' => 'notEmpty'
)
);
The problem is that in another model that has hasOne relationship its controller also inserts data into it. I want to NOT require the title, uri and layout from that page. How do I do it?
I have a Post Model and I set Page values from there.
Array
(
[Post] => Array
(
[title] => data[Post][title]
[body] =>
Post Body
)
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
[Page] => Array
(
[meta_keywords] => data[Page][meta_keywords]
[meta_description] => data[Page][meta_description]
)
)
I do this from the controller to set info for the Page model
$this->data['Page']['title'] = $this->data['Post']['title'];
It turns to be like this:
Array
(
[Post] => Array
(
[title] => data[Post][title]
[body] =>
Post Body
)
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
[Page] => Array
(
[meta_keywords] => data[Page][meta_keywords]
[meta_description] => data[Page][meta_description]
[title] => data[Post][title]
)
)
My problem is that I do not require a Page field when saving. Post belongsTo Page.
I don't require [Page][layout] when saving a Post as a Post uses the default view of the method in the Post Controller. A page uses static pages and require them when creating a Page, not when creating a Post.
You're preprocessing the data before it goes for validation, so you're taking some control away from validation. As you seem to be making the decision (in code) whether or not certain fields need to be artificially populated you are rendering those parts of the validation redundant and you should remove them. If you have multiple validations in php, you're going to end up confused.
Where to do this or how to do it more cleanly? WellbeforeValidate might be the 'correct' place to do this, but I would do it wherever it best fits with the logic of your application. Function should come before elegance.

Resources