Selected Table Update with Database\Selected\Table

3 years ago

thundervoice
Member | 11
+
0
-

Hy Guys

Can you tell me, what is wrong with this Script?

The Exception is:

Nette\InvalidArgumentException

There are more parameters than placeholders.

namespace App\Model;

use Nette;
use Nette\Security\Passwords;


/**
 * Users management.
 */
class UserManager extends Nette\Object implements Nette\Security\IAuthenticator
{
    const
        TABLE_NAME = 'users',
        COLUMN_ID = 'dbuserid',
        COLUMN_USERID = 'userid',
        COLUMN_MND = 'mndid',
        COLUMN_STORE = 'storeid',
        COLUMN_CONTACTID = 'contact_id',
        COLUMN_USERNAME = 'username',
        COLUMN_EMAIL = 'user_email',
        COLUMN_PASSWORD_HASH = 'password',
        COLUMN_ROLE = 'user_role',
        COLUMN_DESC = 'user_desc',
        COLUMN_COLOR = 'user_color',
        COLUMN_HELP = 'user_help',
        COLUMN_USER = 'user_updateuser',
        COLUMN_CREATE = 'user_create',
        COLUMN_UPDADE = 'user_update',
        COLUMN_UPDATEUSER = 'user_updateuser',
        COLUMN_ACTIVE = 'active';


    /** @var Nette\Database\Context */
    private $database;
    private $mandator;
    private $httpRequest;
    private $post;

    public function __construct(Nette\Database\Context $database, Nette\Http\IRequest $httpRequest)
    {
        $this->database = $database;
        $this->httpRequest = $httpRequest;
    }

    // @var $user UserObject
    // function should update the selected User
    // I don't understand, is this the right Way for $this->database->table('users')->update($userdata)?
    public function updateUser($user){
        $data = new \stdClass();
        $userdata = array(
            self::COLUMN_USERNAME => $user->username,
            self::COLUMN_USERID => $user->userid,
            self::COLUMN_EMAIL => $user->user_email,
            self::COLUMN_HELP => $user->user_help,
            self::COLUMN_USER => $user->user_updateuser,
            self::COLUMN_COLOR => $user->user_color,
            self::COLUMN_CONTACTID => $user->contact_id,
            self::COLUMN_ROLE => $user->user_role,
            self::COLUMN_DESC => $user->user_desc,
            self::COLUMN_CREATE => $user->user_create,
            self::COLUMN_UPDADE => $user->user_update,
            self::COLUMN_ACTIVE => $user->active);
        // this should only push to $userdata, when user Password is set new
        if($user->password !== null && $user->password !== ''){
            $userdata['password'] = Passwords::hash($user->password);
            \array_push($userdata, [self::COLUMN_PASSWORD_HASH => Passwords::hash($user->password)]);
            //if the Password is set add this as Parameter to $userdata like this: (password=?) this is wrong right?
        }
        try{
            $this->database->table('users')->update($userdata);
        } catch (Exception $ex) {
            $data->message = "Es ist ein Fehler aufgetreten:".$e;
            throw new \PDOException;
        }
        return $this->getUser($user->userid);
    }

Thanks for helping me!

3 years ago

premek_k
Member | 173
+
0
-

What do you want to do with array_push construction?
Previous row do the trick, or better:

<?php
    $userdata[self::COLUMN_PASSWORD_HASH] = Passwords::hash($user->password);
?>

3 years ago

premek_k
Member | 173
+
0
-

Hey, and one more thing:

don't put a primary key (probably self::COLUMN_USERID => $user->userid) to $userdata, and do it this way:

<?php
    $row = $this->database->table('users')->wherePrimary($user->userid);
    if ($row){
        $row->update($userdata);
    }
?>

Last edited by premek_k (2016-02-08 11:08)