joomla set JTable property to null - joomla

How i can set JTable property too null and save it
my code so far w/o succes:
$table = JTable::getInstance($type='detalegrupy', $prefix='FootsalTable', $config = array());
$table->load($id);
$table->promotion = NULL;
$id_group = $table->id_group;
$table->store();
or how to restore it to it's default value.

i found answer for it,
$table->store(TRUE);
done the work.
sorry for spam

Edit the file administrator/components/YOUR_COMPONENT/tables/NAME_OF_YOUR_TABLE.php and add this function inside the table class:
public function store($updateNulls = true)
{
return parent::store($updateNulls);
}

Related

In Laravel, how do you bind parameters in DB raw query when you set model attribute?

For example, I have my codes.
$news = new News();
$news->title = 'hello world';
$new->user = $user_id,
$news->urlcc = DB::raw('crc32("'.$args['newsShortUrlInput'].'")');
$news->save();
$news->refresh();
Here with attribute $news->urlcc comes from user input after using mysql function crc32();
For the SQL injection issue, above codes not safe.
So, my question is how to bind the parameters in DB::raw() with Laravel model something like below.
$news->urlcc = DB::raw('crc32(:newsShortUrlInput)', ['newsShortUrlInput' => $args['newsShortUrlInput]);
Thanks,
I found one solution, not sure it is right or perfect solution.
In News model class to rewrite setAttribute, see below.
public function setUrlcrcAttribute($shortUrl)
{
$this->attributes['urlcrc'] = $this->getConnection()->select('select crc32(?) as urlcrc', [$shortUrl])[0]->urlcrc;
}
In your service class to create a new model like below.
$news = new News();
$news->title = 'hello world';
$new->user = $user_id,
$news->urlcrc = $args['newsShortUrlInput']; // Laravel model will try to build the real attribute urlcrc
$news->save();
$news->refresh();
It works to me, but not sure this is perfect solution or not.

Modify Laravel request before insert

Is is possible to modify a request before it gets inserted into the database?
public function store(StoreRequest $request)
{
$request->date_posted = strtotime($request->date_posted);
//insert data here.
}
Yes it can be, what you have works perfectly fine
public function store(StoreRequest $request)
{
$request->date_posted = strtotime($request->date_posted);
or
$datePosted = $request->date_posted + 2;
$datePosted = $request->date_posted . 'some other addons';
//insert data here.
}
these are just examples but i hope you get what im saying.
Fyi if you input into the db, the created_at and updated_at will update and so you don't need to insert the time the post was made manually.
Try this solution maybe can resolve your issue . Add setter in model so it will be look like this
Class ModelX {
public function setDateDatePosted Attribute($date_posted)
{
$this->attributes['date_posted'] = strtotime($date_posted);
}
}
hope it useful for you .

Laravel AutoHydrate Ardent Model, but sometimes manually assign?

So I have my Model setup like this:
protected $fillable = array('dragon_id', 'name', 'gender');
public $autoHydrateEntityFromInput = true; // hydrates on new entries' validation
public $forceEntityHydrationFromInput = true; // hydrates whenever validation is called
However in ONE instance, I need to manually assign some of the attributes.. is there a way to do this? Because when I manually assign with auto hydrate set to true, it overwrites what I manually set.
Thanks for any help/suggestions!
I had the same problem, if you set public $forceEntityHydrationFromInput = false; (or comment out completely) then you can call:
$newModel = MyModelClass::create(arrayOfAttributes);
And it will not auto hydrate.

Joomla - Where is the code in K2 which saves a new item's Title and alias

I have looked every where in the administrator\components\com_k2 folder but am not able to find the code that saves a new item\article in K2. I checked the item.php file under models folder. No luck.
I need to override the K2 item save method.
I need a know the exact method that saves the Item's title and alias into the #__K2_content table.
I have to duplicate the K2 items in joomla articles on save and remove on trash/delete.
I have successfully been able to override the K2 core code. But am unable to find the right code to override. (override method is here)
The table that stores the K2 items (at least in the latest K2 version - 2.6.5) is #__k2_items, not #__k2_content.
I went through the code, it looks like K2 uses Joomla's methods: see administrator/components/com_k2/controllers/item.php - line 24: function save(). Everything is extended from Joomla classes.
class K2ControllerItem extends K2Controller
{
public function display($cachable = false, $urlparams = array())
{
JRequest::setVar('view', 'item');
parent::display();
}
function save()
{
JRequest::checkToken() or jexit('Invalid Token');
$model = $this->getModel('item');
$model->save();
}
.....
}
The K2 controller: /administrator/components/com_k2/controllers/controller.php
...
else if (version_compare(JVERSION, '2.5', 'ge'))
{
class K2Controller extends JController
{
public function display($cachable = false, $urlparams = false)
{
parent::display($cachable, $urlparams);
}
}
}
...
#Shaz, you gave me the right direction to look into.
in com_k2\controllers\item.php
this $model->save();saves the data.
The function save() is in the com_k2\models\item.php file, where there are two lines that capture the data.
$row = JTable::getInstance('K2Item', 'Table');
this initiates the $row, while
if (!$row->bind(JRequest::get('post')))
this populates $row.
So now $row contains all the variable values.
Now, this if (!$row->store()) saves the data.
I will use $row to save the same for the Joomla! articles in com_content.
Feels Good :)

Programmatically change product attribute at store view level

I'm sorry if this question is trivial but I've been struggling to find what I am doing wrong here. I am trying to change the value of an attribute on a store view level but the default is also changed whereas it shouldn't be. Of course, this attribute is set up to be "store-view-scoped". To keep it simple, I've tried with the product name. No success.
Below are unsuccessful tests I've tried...
Do you see what I am doing wrong here?
Many thanks.
My tries :
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setStore(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
I even tried by adding the line below before the product model load...
Mage::app()->setCurrentStore(STORE_ID);
So here is the complete snippet to change attribute value for a specific product attribute on a specific store view. Example with the product name :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('my_new_product_name')->save();
And as an additional answer, one could be interested in changing the attribute value to the default one. In this case, the argument 'false' must be passed to the setAttribute :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName(false)->save();
You need to set the current store to admin at the top of your code block:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
note when loading product with data for some store view, also default values get loaded. saving such product will save default values as store values (thus unset "Use Default Value" for fields)
i ended up with following function to clean-up product data from default values
public static function _removeDefaults($item) {
static $attributeCodes = null;
if($attributeCodes == null) {
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
$attributeCodes = array();
foreach($attributes as $attribute) {
$ac = $attribute->getAttributeCode();
if(in_array($ac, array('sku','has_options','required_options','created_at','updated_at','category_ids'))) {
continue;
}
$attributeCodes[] = $ac;
}
}
foreach($attributeCodes as $ac) {
if(false == $item->getExistsStoreValueFlag($ac)) {
$item->unsetData($ac);
}
}
}
remember to send only product loaded for some store view

Resources