I have a laravel 5.2 project and i want to run laravel scheduler by specified time from database data, so i create schedule kernel like this :
protected function schedule( Schedule $schedule )
{
// Run proposal schedule to generate albums
$proposals = Proposal::whereStatus( true )->whereGenerated( false )->get();
foreach ( $proposals as $key => $proposal ) {
$time = date( 'i G ', strtotime( $proposal->duedate ) );
$send_date = date( $time . 'j n * Y', strtotime( $proposal->created_at . ' + ' . $proposal->timeout . ' days' ) );
$schedule->call( function() use( $proposal ) {
$proposal->generateAlbums();
} )->cron( $send_date );
}
}
And it's work fine, until i reset my migration and try to migrate from the begining, i got an error like this
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "proposals" does not exist
So i think it's not good if i place my code there, right??
where should i place my code for this??
Or it's already right and what i need just to check if database table are exists or not??
I need best solution to code just the way laravel provide..
You can check if table exists or not by
if (Schema::hasTable('proposals')) {
//Your code which needs to execute
}
Related
I trying to generate random transaction_id, with format "2000-yymmm-0000".
I already know how to set the transaction_id, but I have a problem with the auto-increment from "2000-yymm-0000" to "2000-yymm-0001", and reseting automatically to "2000-yymm-0000" at every new month.
I put this logic in different path of controller.
PS: I'm using Lumen 6.0 with Laravel 6.0.
I'm trying to create the increment with:
$number = sprintf('%04d', 0000);
$number++;
but it didn't work.
$year = 2000;
$time = date('ym');
$number = sprintf('%04d', 0000);
$transaction_id = $year . '-' . $time . '-' . $number;
$transaction_data = explode('-', $transaction_id);
$month = date("m", strtotime($transaction_data[0]));
I expect the result to automatically increment every time a new data is stored to the database. But the actual result is transaction_id being having always the same value 2000-yymm-0000.
What am I doing wrong?
I know it's weird to answer my own question, but i already find the solution.
in Models/Order.php
i create a function like this.
public function count()
{
$this->where(transaction_id)->count();
}
after that i call the function from Model/Order.php to OrdersController.php
public function create(Request $request)
{
$year = 2000;
$date = date('ym');
$total_data = $this->table->count();
$number = str_pad($total_data + 1, 4, 0, STR_PAD_LEFT);
$transaction_id = $year . '-' . $date . '-' . $number;
return $this->success('count all transaction id', $t_id);
}
I'm trying to loop over multiple servers that need to run 1 at a time using Laravel's withChain. The first job completes just fine but the data I'm passing within the chained jobs gives me the
Attempt to assign property of non-object
When I log out the initial dispatched data it looks just like the constructed data in my array so I'm not sure what I'm doing wrong.
$new_jobs_array = [];
foreach ($this->wasRequest->nodes->sortByDesc('pivot.node_type') as $node) {
if ($node->pivot->node_type != 'WAS_DMGR')
{
$snode = strtolower($node->hostname);
$shortname = strtok($snode, '.');
$fileName = strtolower($mnemonic).'_'.$shortname.'_'.$reqId.'.json';
$sourceJsonPath = base_path() . "/json/was/" . $fileName;
$new_job = 'new BootStrapWasNode('. $node .', '. $this->wasRequest .', '.$sourceJsonPath.')';
array_push($new_jobs_array, $new_job);
} else {
$dmgr_node = $node;
}
}
//Log::info($new_jobs_array);
$dmgr_node_sname = strtok($this->wasRequest->nodes->where('pivot.node_type', 'WAS_DMGR')->pluck('hostname')[0], '.');
$fileName = strtolower($mnemonic).'_'.$dmgr_node_sname.'_'.$reqId.'.json';
$sourceJsonPath = base_path() . "/json/was/" . $fileName;
$this->wasRequest->status = 'Bootstrapping Nodes';
$this->wasRequest->save();
//Log::info("DMGR-------------------".$dmgr_node.", ".$this->wasRequest.", ".$sourceJsonPath);
BootStrapWasNode::withChain($new_jobs_array)->dispatch($dmgr_node, $this->wasRequest, $sourceJsonPath);
I can attach the log view if needed but there is a lot of data for each node. The issue is with the $new_nodes_array, the initial dispatch($dmgr_node,$this->wasRequest,$sourceJsonPath) completes without issue.
Was able to figure out the issue.
This line was incorrect
$new_job = 'new BootStrapWasNode('. $node .', '. $this->wasRequest .','.$sourceJsonPath.')';
It should be
$new_job = new BootStrapWasNode($node, $this->wasRequest, $sourceJsonPath);
i have update query problem in codeigniter.
$arrPartnerId = $partnerData['partner_id'];
print_r($partnerData['partner_id']);
if(is_array($arrPartnerId) > 0 ){
foreach( $arrPartnerId as $partnerId){
$this->db->set('partner_id', $partnerId );
$this->db->where('promotion_id', $promotionData['promotion_id'] );
$this->db->update('partner_promotion_relation');
}
}
my data is $aaPartnerId=([0]=>4,[1]=>5) and i have one id then how to update in database.
I think you confuse a where and set condition, you should rather have this :
$this->db->set('promotion_id', $promotionData['promotion_id']);
$this->db->where('partner_id', $partnerId);
$this->db->update('partner_promotion_relation');
It's just a hypothesis, because in your case you keep update 2 times the same line in your table, which is not logical.
If your array is like this,
$arrPartnerId = array(
0 => 4,
1 => 5
);
And
$promotionData['promotion_id'] = 123; //assumption
Then try this,
if(sizeof($arrPartnerId) > 0 )
{
foreach( $arrPartnerId as $partnerId)
{
$this->db->set('partner_id', $partnerId );
$this->db->where('promotion_id', $promotionData['promotion_id'] );
$this->db->update('partner_promotion_relation');
}
}
It will resolve the problem.
In database I have a column acc_type as int, and I'm trying to code this if $_SESSION['accountTpye']==1 then allowed this user to edit the other users posts,
And should I use and instead of && ? " or , || ".
Example code:
if(isset($_SESSION['username']) && ($_SESSION['username']==$dnn2['author'] || $_SESSION['accc_type']==1['username'])) {
echo "<a href='edit.php'>Edit</a>";
}
i think this will resolve your problem :
if(isset($_SESSION['username']){
if( ($_SESSION['username']==$dnn2['author']) || ($_SESSION['acc_type']==1) ){
echo "<a href='edit.php'>Edit</a>";
}
}
if((isset($_SESSION['username']) && $_SESSION['username']==$dnn2['author']) || $_SESSION['acc_type']==1){
echo "<a href='edit.php'>Edit</a>";
}
This part doesn't make sense:
$_SESSION['acc_type']==1['username']
If you just remove that last ['username'] and then wrap the session username part to be the first part of the conditional so the acc_type one does not depend on it then things should be ok
UPDATE:
In your login.php file locate this part:
$req = mysql_query('select password,id from users where username="'.$username.'"');
$dn = mysql_fetch_array($req);
if($dn['password']==sha1($password) and mysql_num_rows($req)>0)
{
$form = false;
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
You need to also select the acc_type. ASSUMING it's a field in the users table, modify the query to read this:
'select password,id, acc_type from users where username="'.$username.'"'
Then make sure to set the session var:
$form = false;
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
$_SESSION['acc_type'] = $dn['acc_type'];
When you test this, make sure you log out then log bacak in again.
I want to insert dynamic data into my preorder traversal table. I am using rebuild_tree technique by referencing this tutorial : http://www.sitepoint.com/hierarchical-data-database-3/
Here I modified the code as per the need in codeigniter. But when I calling function rebuild_tree in foreach loop it showing the fatal error like :
Fatal error: Maximum function nesting level of '100' reached, aborting! in /var/www/hr/system/database/drivers/mysql/mysql_driver.php on line 199 Call Stack: 0.0389 360600 1. {main}() /var/www/hr/index.php:0 0.0401 468096 2. require_once('/var/www/hr/system/core/CodeIgniter.php') /var/www/hr/index.php:240 0.0689 3798516 3. call_user_func_array() /var/www/hr/system/core/CodeIgniter.php:359 0.0689 3798588 4. Structure->add_structure() /var/www/hr/system/core/CodeIgniter.php:0 0.0689 3798632 5.
structure_model->rebuild_tree() /var/www/hr/application/controllers/test/structure.php:42 0.0701 3928356 6. structure_model->rebuild_tree() /var/www/hr/application/models/test/structure_model.php:35 0.0703 3931572 7. structure_model->rebuild_tree() /var/www/hr/application/models/test/structure_model.php:35 0.0705 3934788 8. structure_model->rebuild_tree() /var/www/hr/application/models/test/structure_model.php:35 0.0706 3938008 9.
CI_DB_driver->query() /var/www/hr/application/models/xome/structure_model.php:29 0.0848 4216988 97. CI_DB_driver->simple_query() /var/www/hr/system/database/DB_driver.php:299 0.0848 4216988 98. CI_DB_mysql_driver->_execute() /var/www/hr/system/database/DB_driver.php:453 0.0848 4216988 99. CI_DB_mysql_driver->_prep_query() /var/www/hr/system/database/drivers/mysql/mysql_driver.php:178
And Following is my code:
function rebuild_tree($parent, $left) {
$resultArr = array();
$parent = $_GET['level'];
$left = $_GET['lft'];
$right = $_GET['rgt'];
$right = $left + 1;
$sql = 'SELECT * FROM subunit WHERE level="' . $parent . '";';
$query = $this->db->query($sql);
$data = $query->result();
foreach ($data as $datap) {
$resultArr['name'] = $datap->name;
$resultArr['level'] = $datap->level;
$right = $this->rebuild_tree($resultArr['level'], $right);
}
$sql = 'UPDATE subunit SET lft=' . $left . ', rgt=' . $right . ' WHERE name="' . $parent . '";';
$query = $this->db->query($sql);
return $right + 1;
}
Is there any solution for this. Thank you.
Be careful when calling the function itself. Often it might end up calling itself forever. You need some kind of limitation.
You can check most of Codeigniters own libraries, and check the initialize(). That's a good example on how to handle a function which calls itself. Basicly it calls itself till no array remains, thus having an end.
In your case if 2 mysql rows are each other's parents or any similiar, you've opened the door for infinite loops.
Solution
The real problem in your function is that you pass an argument which is supposed to get a new value, however you override that value through your $_GET. That means that the 2nd function calls does exactly the same as the first function call, the third the same, the fourth the same and so on. Since the function calls never changes AND calls itself, your problem occur.
Change above part to:
$parent = $this->input->get('level');
$left = ($left ?: $this->input->get('lft'));
$right = $this->input->get('rgt');