Note that there are some explanatory texts on larger screens.

plurals
  1. POMoodle saving data to database from ASSOC ARRAY not Data Object
    primarykey
    data
    text
    <p>so, i have this associative array, the key is the data column and the value is the data itself, other frameworks like kohana uses ORM that automatically creates the private $data = aray() and passes that to the database... so if </p> <pre><code>$class-&gt;data = "value"; $class-&gt;save() // $class-&gt;update() </code></pre> <p>this is easy, in frameworks with ORM, but how about moodle? i tried using $DB->insert_record() but the stdClass is hard creating. how could i pass arrays to the database in moodle? please i need help... here's my class</p> <pre><code>&lt;?php /** * Course completion Extention- student self marked - plugin * * @package completion * @copyright 2012 Ekomerce Ltd * @author Gavin Sanden &lt;gavin@ekomerce.co.uk&gt; */ completion_ext::$DB = $DB; class completion_ext{ private $data = array(); public static $DB; /** * DB Table * @var string $table */ public $table = 'completion_ext'; /** * Array of required table fields, must start with 'id'. * @var array $required_fields */ public static function fetch_all($params) {} function __construct($data = null){ if($data != null &amp;&amp; is_array($data)){ $this-&gt;data = $data; } } public static function get_from_user($user_id){ $variables = self::$DB-&gt;get_records('completion_ext',array( 'user_id'=&gt;$user_id )); $completion_array = array(); foreach($variables as $variable){ $com = new completion_ext($variable); $completion_array[] = $com; } return $completion_array; } public static function exists($user, $course){ return self::$DB-&gt;record_exists('completion_ext', array( 'user_id'=&gt;$user, 'course'=&gt;$course )); } public static function id_exists($id){ return self::$DB-&gt;record_exists('completion_ext', array( 'id'=&gt;$id )); } public static function get_from_course($course, $user_id){ $variables = self::$DB-&gt;get_record('completion_ext',array( 'user_id'=&gt;$user_id, 'course'=&gt;$course )); return new completion_ext($variables); } public function __set($dt, $vl){ $this-&gt;data[$dt] = $vl; } public static function get_by_id($id){ $variables = self::$DB-&gt;get_record('completion_ext',array( 'id'=&gt;$id )); return new completion_ext($variables); } public static function check_completion_from_user($user_id){ self::$DB-&gt;record_exists($table, array( 'user_id'=&gt;$user_id )); } public function __get($dt){ return $this-&gt;data[$dt]; } public function save(){ echo print_r($this-&gt;data); } public function update(){ self::$DB-&gt;update_record("completion_ext", $this); } </code></pre> <p>} ?></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload