I am getting the following exception
PHP Fatal Error – yii\base\ErrorException
Maximum execution time of 60 seconds exceeded !!!!!!!
public $password;
public function generateAuthKey()
{
return $this->auth_key = Yii::$app->security->generateRandomString();
}
public function setPassword($password)
{
return $this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
public function save($runValidation = true, $attributeNames = null)
{
$user = new Users();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
$this->id = $user->id;
return true;
}
}
in your User model you should canghe the function name save() with another name eg saveUser so this is not recalled by the inner $user->save() that is a normal ActiveRecord function for save.
Calling User->save() as you are doing you produce an infinite nested loop ..
.....
public function saveUser($runValidation = true, $attributeNames = null)
{
$user = new Users();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
$this->id = $user->id;
return true;
}
}
...
In your controller Action eg:action Create you should call your $user->saveUser(...)
*/
public function actionCreateForCatone2()
{
....
$user->saveUser(...);
...
}
Related
I have modified my signup form, I add a field that called billCode, so I want add a function to my signForm model to random integer number, I have a function to random integr and I put that into signup form model like this
class SignupForm extends Model {
// ...
// ...
public static function randomNumber()
{
$randBill = '';
for ($i=0; $i < 8; $i++){
$randBill .= mt_rand(0, 9);
}
return $randBill;
}
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
$user->billCode = randomNumber();
return $user->save() ? $user : null;
}
}
when I press the submit button in signUp Form, yii2 keep give me this error
Call to undefined function frontend\models\randomNumber()
Any help?
Copy from comment
randomNumber is a static function, you can't call it just by randomNumber(). Using self::randomNumber() could be work.
You can call it by self keyword
class SignupForm extends Model {
// ...
// ...
public static function randomNumber()
{
$randBill = '';
for ($i=0; $i < 8; $i++){
$randBill .= mt_rand(0, 9);
}
return $randBill;
}
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
//$user->billCode = $this->randomNumber();
$user->billCode = self::randomNumber();
return $user->save() ? $user : null;
}
}
I'm making a social network's login page but when I login a get the error above. My db.php is (i use pdo):
<?php
class DB {
private static function connect() {
$pdo = new PDO('mysql:host=127.0.0.1;dbname=social;charset=utf8', 'danny', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public static function query($query, $params = array()) {
$statement = self::connect()->prepare($query);
$statement->execute($params);
if (explode(' ', $query)[0] == 'SELECT') {
$data = $statement->fetchAll();
return $data;
}
}
}
?>
Called from
$user_id = DB::query('SELECT id
FROM users
WHERE email=:email')[0]['id'];
DB::query('INSERT INTO login_tokens
VALUES(\'\', :token, :user_id)',
array(':token'=>sha1($token), ':user_id'=>$user_id));
Your class has many issues. to fix them:
class DB {
protected static $pdo;
public static function connect() {
static::$pdo = new PDO('mysql:host=127.0.0.1;dbname=social;charset=utf8', 'danny', 'Dani2034');
static::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public static function query($query, $params = array()) {
$statement = self::$pdo->prepare($query);
$statement->execute($params);
return $statement;
}
}
DB::connect(); // called only once
$user_id = DB::query('SELECT id FROM users WHERE email=?', array($email))->fetchColumn();
DB::query('INSERT INTO login_tokens VALUES(null, ?, ?)', array(sha1($token), $user_id));
I have very simple users database: user_id, user_name, user_email
My model this:
class Users extends CI_Model {
private $table;
private $table_fields;
private $table_fields_join;
function __construct() {
parent::__construct();
$this->table = 'users';
$this->table_fields = array(
$this->table.'.user_id',
$this->table.'.user_name',
$this->table.'.user_email'
);
$this->table_fields_join = array();
}
function select(){
$this->db->select(implode(', ', array_merge($this->table_fields, $this->table_fields_join)));
$this->db->from($this->table);
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result();
} else {
return false;
}
}
function insert($data) {
$data = array(
'user_name' => $data['user_name'],
'user_email' => $data['user_email']
);
$this->db->insert($this->table, $data);
}
My controller this:
class Users extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('users');
}
public function select(){
$data['query'] = $this->users->select();
$data = json_encode($data['query']);
echo $data;
}
public function insert($json){
$data = json_decode($json);
$this->users->insert($data);
}
}
And this is my routing.php:
$route['default_controller'] = 'Welcome';
$route['users'] = 'users/select';
$route['users/insert/:(any)'] = 'users/insert';
I would like that 127.0.0.1/users/select give json.
Example: [{"user_name":"user1","user_email":"user#user.de"}]
This JSON insert my table: 127.0.0.1/users/insert/[{"user_name":"user1","user_email":"user#user.de"}]
But my code is not working. :-(
You want to return json object in response, so it's required to set json type in response header. As given here
public function select(){
$data['query'] = $this->users->select();
$this->output
->set_content_type('application/json')
->set_output(json_encode($data['query']));
}
It is required to encode part as below for insert part. so you can use this generated url to call your insert.
site_url('usres/insert/'.urlencode('[{"user_name":"user1","user_email":"user#user.de"}]'));
your insert route should be as
$route['users/insert/:(any)'] = 'users/insert/$1';
your insert method should be updated as
public function insert($json){
$json = urldecode($json);
$data = json_decode($json);
$this->users->insert($data);
}
}
I finished transitioning to PDO and am modifying my system according to fatal errors that pop up.
ERR: Call to a member function execute() on a non-object in /home/a1933806/public_html/globals/server-bin/php/core.php
LINE: 43
This is my setup with affected line in bold:
class netCore {
private $armor;
private $boot;
private $dbHost = "*****.*******.com";
private $dbNAME = "********";
private $dbPASS = "********";
private $dbUSR = "********";
private $err;
private $state;
public function __construct() {
$bootloadr = "mysql:host=".$this->dbHost.";dbname=".$this->dbNAME.";charset=UTF8";
$opt = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
try {
$this->boot = new PDO($bootloadr, $this->dbUSR, $this->dbPASS, $opt);
} catch(PDOException $e) {
$this->err = "<b>Lebensborn® netCore™ Error:</b> An exception has been raised during Network-to-Database procedures.<br /><b>Message:</b> ".$e->getMessage().".";
}
}
public function bind($param, $value, $type = null) {
if(is_null($type)) {
switch(true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->state->bindValue($param, $value, $type);
}
public function exe() {
return $this->state->execute();
}
public function count() {
return $this->state->rowCount();
}
public function q($q) {
try {
$this->armor = $this->boot->prepare($q);
$this->state = $armor;
} catch(PDOException $e) {
$this->err = "<b>Lebensborn® netCore™ Error:</b> An exception has been raised during Network-to-Database procedures.<br /><b>Message:</b> ".$e->getMessage().".";
}
}
public function set() {
$this->exe();
return $this->state->fetchAll(PDO::FETCH_ASSOC);
}
public function single() {
$this->exe();
return $this->state->fetch(PDO::FETCH_ASSOC);
}
public function transBegin() {
return $this->boot->beginTransaction();
}
public function transCancel() {
return $this->boot->rollBack();
}
public function transEnd() {
return $this->boot->commit();
}
}
i have made simple database application in zend framework.in that i have made user class and defined different function for database interaction.this function are called from controller class..but i have to declare each time class object for calling the method of user class in each action.how can i declare user class object one time and use in all action method of controller.
here is my controller page
class UserController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
Zend_Session::start();
}
public function indexAction()
{
$title = Zend_Registry::get('title');
$this->view->assign('name', 'Wiwit');
$this->view->assign('title', $title);
}
public function logoutAction()
{
Zend_Session::destroy();
$this->redirect("/user/login");
}
public function registerAction()
{
$this->view->assign('title','Register');
$request = $this->getRequest();
$this->view->assign('action', $request->getBaseURL()."/user/register");
if($request->isPost())
{
$data = array('first_name' => $request->getParam('first_name'),
'last_name' => $request->getParam('last_name'),
'user_name' => $request->getParam('user_name'),
'password' => $request->getParam('password')
);
$user = new Application_Model_User();
$rows_affected=$user->user_insert($data);
$this->redirect("/user/login");
}
}
public function loginAction()
{
if(isset($_SESSION['username']))
{
$this->redirect("/user/home");
}
$this->view->assign('title','Register');
$request = $this->getRequest();
$this->view->assign('action', $request->getBaseURL()."/user/login");
if($request->getParam('msg'))
{
$this->view->msg='username or password is invalid';
}
if($request->isPost())
{
$data = array(
'user_name' => $request->getParam('user_name'),
'password' => $request->getParam('password')
);
$user = new Application_Model_User();
$result=$user->user_login($data);
if(empty($result)){
$this->redirect("/user/login/msg/login failed");
}
else{
$_SESSION['username']=$result->user_name;
$this->redirect("/user/home");
}
}
}
public function homeAction()
{
if(isset($_SESSION['username']))
{
$this->view->title='home';
$request = $this->getRequest();
$user = new Application_Model_User();
$result=$user->user_grid();
$this->view->rows=$result;
}
else
{
$this->redirect("/user/login");
}
}
public function editAction()
{
$request = $this->getRequest();
$id= $request->getParam("id");
$user = new Application_Model_User();
$result=$user->user_edit($id);
$this->view->assign('data',$result);
$this->view->action= $request->getBaseURL()."/user/update";
}
}
and here is my user class
<?php
class Application_Model_User extends Zend_Db_Table
{
public function user_insert($data)
{
global $DB;
$rows_affected = $DB->insert('user', $data);
return $rows_affected;
}
public function user_login($data)
{
$u_name=array_shift($data);
$pass=array_shift($data);
$select = $this->_db->select()
->from('user')
->where("user_name = ?",$u_name)
->where("password = ?",$pass);
$result = $this->getAdapter()->fetchRow($select);
return $result;
}
public function user_grid()
{
global $DB;
$sql = 'SELECT * FROM user';
$stmt = $DB->query($sql);
$result = $stmt->fetchall();
return $result;
}
public function user_edit($data)
{
$select = $this->_db->select()
->from('user')
->where("id = ?",$data);
$result = $this->getAdapter()->fetchRow($select);
$data = (array) $result;
return $data;
}
public function userupdate($data,$id)
{
global $DB;
$rows_affected =$DB->update('user', $data,'id = '.$id);
return $rows_affected;
}
public function userdelete($id)
{
global $DB;
$rows_affected =$DB->delete('user','id = '.$id);
return $rows_affected;
}
}
?>
You can create your user object in the init() method, and store it as a class variable.
public function init()
{
/* Initialize action controller here */
Zend_Session::start();
$this->user = new Application_Model_User();
}
Then it's available in any action method.
public function homeAction()
{
if(isset($_SESSION['username']))
{
$this->view->title='home';
$request = $this->getRequest();
$result = $this->user->user_grid(); // changed
$this->view->rows=$result;
}
else
{
$this->redirect("/user/login");
}
}