• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use of Unique in mySQL field cases Error

#11
Ok, here is another one I found that you can try.

PHP Code:
// Usage:
$data = array(
    'item'     => $post_array['employee_title'],
    'campaign' => $this->session->userdata('campaign'),
    'userid'   => $this->session->userdata('userid')
);

$this->insertIgnore($data); 

Place this into your model:

PHP Code:
// Add this to your model.

/**
 * Prepare INSERT IGNORE SQL query
 *
 * @param Array $data Array in form of "Column" => "Value", ... 
 * @return Null
 */
protected function insertIgnore(array $data)
{
    $_prepared = array();

    foreach ($data as $col => $val)
    {
        $_prepared[$this->db->_escape_identifiers($col)] = $this->db->escape($val);
    }

    $this->db->query('INSERT IGNORE INTO `titles` ('.implode(',',array_keys($_prepared)).') VALUES('.implode(',',array_values($_prepared)).');');


You may need to change it from protected to public if your not calling it from within.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#12
I am getting this error:

ERROR - 2020-07-25 16:34:30 --> Severity: error --> Exception: syntax error, unexpected 'protected' (T_PROTECTED), expecting end of file /app/application/models/MyModel.php 16

I also tried public and I get this:

ERROR - 2020-07-25 16:36:27 --> Severity: error --> Exception: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file /app/application/models/MyModel.php 16

Here is MyModel.php from models
````
<?php
/**
* Created by PhpStorm.
* User: richb201
* Date: 12/16/2017
* Time: 3:57 PM
*/
// Add this to your model.

/**
* Prepare INSERT IGNORE SQL query
*
* @param Array $data Array in form of "Column" => "Value", ...
* @return Null
*/
public function insertIgnore(array $data)
{
    $_prepared = array();

    foreach ($data as $col => $val)
    {
        $_prepared[$this->db->_escape_identifiers($col)] = $this->db->escape($val);
    }

    $this->db->query('INSERT IGNORE INTO `titles` ('.implode(',',array_keys($_prepared)).') VALUES('.implode(',',array_values($_prepared)).');');
}
?>

````
proof that an old dog can learn new tricks
Reply

#13
Here is the whole class for it, I had to track it down. Just make the changes that you need.

PHP Code:
<?php
class Users extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function test() {
        $_data = array(
            'field1' => 'value1',
            'field2' => 'vallue2',
            'field3' => 'value3'
        );

        $this->insert_ignore($_data);
    }

    /**
    * Prepare INSERT IGNORE SQL query
    * @param Array $data Array in form of "Column" => "Value", ... 
    * @return Null
    */
    protected function insert_ignore(array $data) {
        $_prepared = array();

        foreach ($data as $col => $val)
            $_prepared[$this->db->_escape_identifiers($col)] = $this->db->escape($val); 

        $this->db->query('INSERT IGNORE INTO `table` ('.implode(',',array_keys($_prepared)).') VALUES('.implode(',',array_values($_prepared)).');');
    }



And here is another one I found:

PHP Code:
public function insertIgnore(array $data)
{
    $sql $this->db->insert_string('titles'$data);
    $sql str_replace('INSERT INTO''INSERT IGNORE INTO'$sql);

    $this->db->query($sql);

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#14
I guess more of my problem is with the model. I have named it MyModel.php and load it with $this->load->model('MyModel') in the controller's __construct? And then just call insertIgnore() in one of my callbacks? I also added another function to MyModel called public function update_population(). At the top of this model file containing the two functions, insertIgnore() and update_population() I have

class MyModel extends CI_Model {

    public function __construct() {
        parent::__construct();
    }


Well, I managed to get the function in the model to run, update_population().  Now I just need to figure out how to set up the SQL.

I still need to get the insertIgnore going....

thx for your help
proof that an old dog can learn new tricks
Reply

#15
One more thing. I see that in the model I don't have access to "$this". So how can manipulate $this->db-> ??
proof that an old dog can learn new tricks
Reply

#16
If the class extends the Model class you should have access to $this->db
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#17
I changed the class in model from CI_Model (as in the docs) to Model here:
in the top of the model:

class MyModel extends Model {

    public function __construct() {
        parent::__construct();
    }

Now I get:
A PHP Error was encountered
Severity: Error

Message: Class 'Model' not found

Filename: models/MyModel.php

Line Number: 9

Backtrace:

Any ideas? Do I need to pass $this into the model? like this:
$this->MyModel->update_population($this);  //this marks in the population or not from exclude
proof that an old dog can learn new tricks
Reply

#18
Did you add the use to your model?

PHP Code:
use CodeIgniter\Model

Add that above the class MYModel

You also need to add the namespace for your model

PHP Code:
namespace App\Models

If thats where it is.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#19
I put the CodeIgniter\Model; into the top of the model. When I run that it seems to work fine except the "$this" still doesn't appear in the model. 

I do see db under CI in the model. Do I perhaps need to open a separate connection to the mySQL table? 

Then I pasted namespace application\models; into the top of the controller.
require 'vendor/autoload.php';
namespace application\models;

I get this error when i do this:
Severity: Compile Error

Message: Namespace declaration statement has to be the very first statement or after any declare call in the script

Filename: controllers/Configure.php

Line Number: 4

Backtrace:

Strangely, although $this is {MyModel}[0] in the debugger in the model, it appears to be working (not the code but at least the first few lines, see below).
$this->db->where('email', $_SESSION['userid']);
$this->db->where('campaign', $_SESSION['campaign']);
$this->db->select('*');
$query = $this->db->get('stat_sample');


how can that be?  Well, somehow it is working! 
proof that an old dog can learn new tricks
Reply

#20
Remove the this and just use the $db and see if it works.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.