Sackgesicht VIP
Total posts: 1,636
07 Фев 2013 02:24

What is the best way update a record through a script, if i know the record_id?

Example:

I want to update the category or a field value of a record without going through the edit form

Последние изменения: 02 Март 2014


Konstantin
Total posts: 1,113
07 Фев 2013 03:32

The best way is update through the edit form. Because, there are many post-save update scripts. Updating through a script don't guarantee success result.


Sackgesicht VIP
Total posts: 1,636
07 Фев 2013 06:19

I have a list of record_id's (array) and want to change all their categories from cat1 to cat2 by a button instead of opening one after the other and save them. Now I just display them as a list, but it would be nice to process them (and change the category ) with one click.

function getProblemRecords()

{

    $ar = array ();

    $db = JFactory::getDbo () ;

    $query = $db->getQuery ( true ) ;

    $query->select ( "record_id" ) ;

    $query->select ( "(select title from #__js_res_record where id = record_id) as title" ) ;

    $query->from ( "#__js_res_record_values" ) ;

    $query->where ( "field_key in ('kdcb94f19759ad03128edcf1e6d91983e', 'kb2b19e140bc73a2d82fa6e7f732d84ef')" ) ;

    $query->where ( "field_value < CURDATE()" ) ;

    $query->where ( "record_id IN (SELECT record_id FROM `#__js_res_record_category` WHERE catid = 2)" ) ;

    $query->group ("record_id") ;

    $db->setQuery ( $query ) ;

    $ar [] = $db->loadAssocList() ;

    foreach ($ar[0] AS $field):?>

        <a href="/<?php echo JRoute::_(JRoute::_(Url::record($field['record_id']) ) ) ;?>">

                <?php echo $field['title'];?>

        </a>

    <?php 

    endforeach;

    return ;

}        

Sergey
Total posts: 13,748
08 Фев 2013 00:06

I can give you code for special task like we did before. Mass change will be added later. May be even in Cobalt 9.


pepperstreet VIP
Total posts: 3,837
13 Фев 2013 15:19

Is it possible to show/debug cobalt operations? Maybe with J! core features or by adding a Debug Toggle in Cobalt backend? Something that displays SQL actions in a template debug position etc.


Sergey
Total posts: 13,748
14 Фев 2013 01:37

Here is the code

public function changecategories()

{

$ar = array();

$db = JFactory::getDbo();



$query = $db->getQuery(true);

$query->select("record_id");

$query->from("#__js_res_record_values");

$query->where("field_key in ('kdcb94f19759ad03128edcf1e6d91983e', 'kb2b19e140bc73a2d82fa6e7f732d84ef')");

$query->where("field_value < CURDATE()");

$query->where("record_id IN (SELECT record_id FROM `#__js_res_record_category` WHERE catid = 2)");

$query->group("record_id");



$db->setQuery($query);

$ids = $db->loadColumn(); $ids[] = 0;

$ids = array_unique($ids);

JArrayHelper::toInteger($ids);



$new_cat = json_encode(array(3 => 'Category name'));



$sql = "UPDATE #__js_res_record SET categories = '{$new_cat}' WHERE id IN (".implode(',', $ids).")";

$db->setQuery($sql);

$db->query();

$sql = "UPDATE #__js_res_record_category SET catid = 3 WHERE record_id IN (".implode(',', $ids).")";

$db->setQuery($sql);

$db->query();

}

Do not forget to change $new_cat and catid = 3.

This will work as tark in controller. You may set cronjob

wget http://rsd/index.php?option=com_cobalt& ;task=cron.changecategories

But I do not remember what was the name of controller.

Работает на Cobalt