Fatal error: Call to a member function format() on a non-object in C:\xampp\htdocs\esupport\application\controllers\order.php on line 234 - codeigniter-2

I am learning CodeIgniter with the following code:
private function getdbdate($date){
$myDateTime = DateTime::createFromFormat('d/m/Y', $date);
return $myDateTime->format('Y/m/d');
}
private function getviewdate($date){
$myDateTime = DateTime::createFromFormat('Y-m-d', $date);
return $myDateTime->format('d/m/Y');
}
This code gives me a fatal error:
fatal error:-call to a member function format() in C:\xampp\htdocs\esupport\application\controllers\order.php on line 234
Can you help me?

Check if $myDateTime is false, using var_dump:
var_dump($myDateTime);
DateTime::createFromFormat() returns false, in case an error occured.

Related

Lumen Artisan command throws "Error: Call to a member function assertExitCode() on int" while testing

my code is as follows:
class DailyUsageCommandTest extends TestCase
{
public function testDailyUsageCommandTest()
{
$this->artisan('daily-usage')->assertExitCode(0);
}
}
But it throws:
1) DailyUsageCommandTest::testDailyUsageCommandTest
Error: Call to a member function assertExitCode() on int
The error is saying that $this->artisan('daily-usage') returns an int (Call to a member function assertExitCode() on int). So you have to store the result and do assertTrue with your value like this:
$result = $this->artisan('daily-usage');
$this->assertTrue($result === 0);

cant access method name from variable

I m trying to use the method name from variable as below but however can't getting success in it. code is as below
$function_name = $this->input->post('class_name');
if(method_exists($this, $function_name))
{
$foo = $this;
$foo->$function_name();
}
else
{
show_404();
}
But keep getting the error of Method name must be a string
Fatal error: Call to undefined method trialdemo::() in D:\xampp\htdocs\trialdemo\application\controllers\trialdemoapp.php on line 38
Try this
if(method_exists($this, $function_name))
{
call_user_func($function_name);
}
else
...
In addition to call_user_func you might want to read about call_user_func_array and is_callable.

Laravel Relationship Collection iteration returns boolean

I have a recursive relationship (sections and sub sections)
defined as this in ReportSection model:
function sub_sections() {
return $this->hasMany('App\ReportSection', 'parent_id');
}
and I'm trying to iterate through it like so:
$section = Section::find($id);
\DB::beginTransaction();
try {
foreach(ReportForm::unlockedForm($section->form_id)->get() as $report) {
foreach($report->sections()->where('section_id', $section->id)->get() as $reportSections) {
\Log::info($reportSections);
foreach($reportSections as $rSection) {
\Log::info($rSection);
foreach($rSection->sub_sections as $subSection) {
The line \Log::info($reportSections); gives {"id":3,"report_form_id":1,"name_en":"DDD","name_fr":"DDD","created_at":"2016-11-29 07:47:24","updated_at":"2016-11-29 07:47:32","section_id":118,"parent_id":1,"order":99,"hidden":0} as expected
but the iterating through it somehow gives a boolean \Log::info($rSection); gives 1
The last line foreach($rSection->sub_sections as $subSection) { gives the error 'Trying to get property of non-object'
Why would iteration through a relationship collection give a boolean? What am I doing wrong?
Edit: changed sub_sections() to sub_sections but the error is still present
You should call the attribute name not the method:
foreach($rSection->sub_sections as $subSection)
{}
Ok after taking a break I was able to figure out that the problem was I was iterating through the same collection twice.
Instead of
foreach(ReportForm::unlockedForm($section->form_id)->get() as $report) {
foreach($report->sections()->where('section_id', $section->id)->get() as $reportSections) {
foreach($reportSections as $rSection) {
It should have been
foreach(ReportForm::unlockedForm($section->form_id)->get() as $report) {
foreach($report->sections()->where('section_id', $section->id)->get() as $rSection) {

Doctrine is converting string to hex value

I am converting one database to another database through a script.
While running the script I am getting the following error:
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xBFbangl...' for column 'country' at row 1
An exception is thrown where it shows that it tries to set country as "\xcf\xbb\xbf\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68".
When I var_dump the value, it just shows as "Bangladesh".
Is there something I have to adjust in my php code?
I have tried this, but phpmyadmin is throwing an #1064 error stating a syntax error in the query.
UPDATE
The scripts is a command in Symfony3:
<?php
namespace My\Bundle\Command;
use My\AccommodationBundle\Entity\Visit;
use My\NewAccommodationBundleV2\Entity\Visit as Visit2;
use My\OtherBundle\Entity\Person;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class VisitConvertToNewFormatCommand extends ContainerAwareCommand
{
private $em;
protected function configure()
{
$this
->setName('accommodation:visit:convert')
->setDescription("Converting old structure to new structure'")
;
}
protected function getTimeStamp() {
$currentTime = new \DateTime('now');
return '['.$currentTime->format('Y-m-d H:i:s').'] ';
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln($this->getTimeStamp().$this->getDescription());
$this->em = $this->getContainer()->get('doctrine')->getManager();
$visits = $this->em->getRepository('MyOldAccommodationBundle:Visit')->findAll();
foreach ($visits as $visit) {
$visit2 = new Visit2();
$visit2->setArrivalDate($visit->getArrivalDate());
$visit2->setArrivalStatus($visit->getArrivalStatus());
$visit2->setArrivalTime($visit->getArrivalTime());
$visit2->setBookingType($visit->getBookingType());
$visit2->setCountry($visit->getCountry()); // <----
...
$this->em->persist($visit2);
$user = $visit->getUser();
if ($user != null) {
$person = new Person();
...
$person->setFirstName(trim(ucfirst(strtolower($user->getFirstName()))));
$person->setLastName(trim(ucfirst(strtolower($user->getLastName()))));
$person->setEmail(preg_replace('/\s+/', '', $user->getEmail()));
...
$this->em->persist($person);
}
}
}
}
\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68 is Bangladesh; the \xcf\xbb\xbf before it makes no sense. In latin1 it is Ï»¿. It does not validly convert to utf8. CFBB is ϻ (GREEK SMALL LETTER SAN), but then bf is broken utf8.
I suggest it is coming from the source of the data.
var_dump probably showed nothing because of what device you were displaying it on.
Ok after days of trying this did the trick:
$country = iconv("UTF-8", "ISO-8859-1//IGNORE", $country);
I have no idea why it went wrong in the first place. if anybody knows, please share.

Fatal error: Call to a member function check_user() on a non-object in C:\xampp\htdocs\TugasWeb2\application\controllers\login.php on line 43

whats wrong with my code ?
$count=$this->db_ecommerce->check_user($user, $pswd)->num_rows(); <-- this line 43
$user=$this->input->post('username');
$pswd=$this->input->post('password').$this->config->item("key_login");
//$pswd=$this->input->post('password');
$count=$this->db_ecommerce->check_user($user, $pswd)->num_rows();
if($count>0) {
$this->session->set_flashdata('status_login', 'ok');
redirect('member');
before you get the num_rows() you should check if there is any result:
$count = $this->db_ecommerce->check_user($user,$pswd);
if($count){
$count = $count->num_rows();
}
else{
$count = 0;
}
Assuming that you have already a model with db_ecommerce as name, you should try loading it :
$this->load->model('db_ecommerce');
$count=$this->db_ecommerce->check_use ...

Resources