I'm deploying my application to my production environment and it's not working as expected. I've narrowed the issue down to one line inside this loop in my controller;
foreach($temp_table_data as $a_payment) {
//array_push($payments, $a_payment->payment); //big collection object
array_push($payments, $a_payment->payment->first()->attributesToArray()); //smaller object
}
The error I get is call to a member function attributesToArray() on a non object. This seems crazy to be because - as the old saying goes - it works fine on my machine.
My dev. environment is Ubuntu trusty64 on PHP 5.5.21 and my production is RedHat Linux PHP 5.5.11. I thought these differences were very minor (maybe I'm wrong?).
If I do a print_r($temp_table_data() then I get a big collection returned. The same on both servers. So at some point it just stops liking either payment (that's a method) or first()
Here is partial of my TempTable.php Model with the payment method;
public function payment(){
return $this->hasMany('App\Models\Payment', 'Vendor ZIP', 'postcode');
}
And my Payment.php model (part of it);
class Payment extends Model {
protected $table = 'headquarters_data';
public function tempTable()
{
return $this->belongsTo('App\Models\TempTable', 'postcode', 'Vendor ZIP');
}
One of the tempTable models doesnt have a Payment and the attributesToArray() method is failing.
Try this and see if it works.
foreach($temp_table_data as $a_payment) {
$payment = $a_payment->payment->first();
if(!is_null($payment)){
$payments[] = $payment->attributesToArray();
}
}
the problem is that in production you have probably changed the data in your database and calling to first() method returns null then you are trying to call attributesToArray() on a null, which is wrong!
you should do a isset() function before calling attributesToArray().
if(isset($a_payment->payment->first()))
array_push($payments, $a_payment->payment->first()->attributesToArray());
Related
<?php
include(APPPATH.'/libraries/REST_Controller.php');
class Quiz extends REST_Controller{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function user_get()
{
$this->load->model('Quizmodel');
$data = $this->Quizmodel->getAll();
$this->response($data, 200);
}
function restclient()
{
$this->load->library('rest', array(
'server' => 'http://localhost/CodeIg/index.php/quiz/'
));
$userr = $this->rest->get('user','','json');
echo $userr;
}
}
?>
I am able to get JSON output if I type http://localhost/CodeIg/index.php/quiz/user in my browser, however if I type http://localhost/CodeIg/index.php/quiz/restclient it gives this error: {"status":false,"error":"Unknown method"}
I tried changing get to post but still the same error.
I referred this page https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814 to do it.
You pinged me on GitHub, even though I haven't used or even thought about this code in at least 4 years.
https://github.com/chriskacerguis/codeigniter-restserver/blob/d19dc77f03521c7a725a4555407e1e4e7a85f6e1/application/libraries/REST_Controller.php#L680
This is where that error is being triggered. Throw a few breakpoints in there or var_dump()'s until you see what is causing the trouble.
You probably want to get off CodeIgniter though, and use something more actively maintained like SlimPHP or Lumen.
firstly I want as you have loaded rest api and created your controller quiz as an api to call , where you can only create your functions like user_get or restclient_get and access them the same manner you are doing.Just change you function name restclient to restclient_get then it will call instead it is even not running at this moment.
Slightly odd one here.
I have Persons and Actions. Persons can have many Actions, while each Action belongs to only one Person. I'm using Chumper's Datatables to display a list of people, including a count of their actions.
Since migrating to a production (forge) server, I'm getting
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Class 'action' not found
when calling the datatable. The error shown
/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:721
public function hasOne($related, $foreignKey = null, $localKey = null)
{
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
suggests it's a problem with my hasMany relationship:
# /models/Person.php
class Person extends Eloquent {
public function actions()
{
return $this->hasMany('Action');
}
# /models/Action.php
class Action extends Eloquent {
public function person()
{
return $this->belongsTo('Person', 'person_id');
}
I assume these are fine, however, as it all works locally. Datatables also works fine elsewhere, calling through other items and their related actions with no trouble.
I've tried composer dump-autoload and artisan dump-autoload on the production server, to no avail. The deployment script on forge is below:
git pull origin master
composer install
php artisan migrate --env=production
I can't tell if it's a config issue, a database issue, a code issue or something else entirely. I've been back through the many similar questions but nothing's jumped out. Any help much appreciated.
for who may have the same problem, triple check the casing of your model! I had it wrong, that's why locally on mac was working but not on the server
So I think I'd borked this one myself.
I'd been lazy and left function datatablePersons() in PersonsController.php using an old 'count' method, relying on long-defunct relationships (that caused n+1, so had to be binned), hence it wobbling over an actions class whenever that relationship was called upon.
Datatable functions in other controllers (with a cleaner 'count' method) work fine, so I've just rewritten datatablePersons() to use the same method.
I've not quite got the query right (in eloquent, at least) yet - see this question here: mysql join ON and AND to laravel eloquent - but the class not found error has certainly gone away.
I'm (massively) guessing that the classmap on the local machine hadn't been flushed since whatever was removed was removed, while the production machine is rebuilt every push, hence the disparity...?
Either way, it's no longer an issue.
So i am working on flex 4.6(before updating to 4.11) on a new project with php services. There is an auto-generated class called MarkerService.as which contains the following:
protected override function preInitializeService():void
{
super.preInitializeService();
super._serviceControl.endpoint = "http://www.mydomain.gr/gateway.php";
}
However i am building a login system that changes dynamically the server in which the user will connect. So i changed the above to:
public var targetServer:String="test1";
protected override function preInitializeService():void
{
super.preInitializeService();
super._serviceControl.endpoint = targetServer;
trace(targetServer+" started");
}
public function setTargetServer(s:String):void
{
targetServer=""+s;
super._serviceControl.endpoint = targetServer;
trace(s+" targeted");
}
And then call my setter on an MXML class when the login method is called. However, it seems like the prInitializeService is called during the start of the execution and thus i get a connection error. I attach the logs from the console:
test1 started <--- when app is executed
test1 started <--- when login button is pressed and setter is called
http://www.mydomain.gr/gateway.php targeted
http://www.mydomain.gr/gateway.php started
Send failed
Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed: url: 'http:test1'
app:/test1
it seems like the new link is not updated after it is setted and the old test1 is used for the connection. Any ideas?
Well after a few hours of testing and changing the logic, it seems like that the assignment of the endpoint must happen before initializing the _serviceControl. I was trying to implement my logic using states which was buggy. However, when i moved the call to the setter on a new view (loginView) everything worked like a charm. From the looks of it, makes a direct call to the service and it is executed before the app is even displayed.
As a result, the solution for changing dynamically the server your gateway is located, is as follows:
Markers.as (or whatever you call it), changed
From
protected override function preInitializeService():void
{
super.preInitializeService();
super._serviceControl.endpoint = "http://www.mydomain.com/gateway.php";
}
To
public var targetServer:String="";
protected override function preInitializeService():void
{
super.preInitializeService();
super._serviceControl.endpoint = "http://www.mydomain.com/gateway.php";
}
public function setTargetServer(s:String):void
{
targetServer=s;
super._serviceControl.endpoint = targetServer;
//trace(s+" targeted");
preInitializeService();
}
And in your login.mxml
var m:services.markersservice.MarkersService=new services.markersservice.MarkersService();
m.setTargetServer("http://www.mydomain.com/gateway.php");
navigator.pushView(HomeView);
I hope this will help more Flex developers.
I am using Laravel and I have just moved my code that is working locally into Live and am getting the exception below within my 'User' controller:
Unhandled Exception
Message:
Using $this when not in object context
The strange thing is, that this IS a class and works locally fine so I don't expect the solution to be using the static notation. It's only when I promote this to Live that I get this error. Could it be that something in the Laravel core that is missing from Live that is not loading the controller class properly?
Has anyone experienced this after promoting their code to Live? any ideas?
UPDATED: snippet of code where error is occuring. remember this code works locally so i believe something is missing rather than this code needs to be changed to fix this issue.
class User_Controller extends Base_Controller {
...
public function action_register() {
...
if ($user) {
//Create the Contact
DB::transaction(function() use ($user_id) {
$org = $this->create_org($user_id); //failing on this line with exception. btw $user is created fine
$this->create_contact($org->id);
$this->create_address($org->id);
});
private function create_org($user_id) {
$result = Org_type::where('name','=',$_POST['org_type'])->first();
$org = Org::Create(
array(
'name' => $_POST['org_name'],
'user_id' => $user_id,
'org_type_id' => $result->id,
)
);
return $org;
}
...
It seems the problem is that you're using $this inside a Closure you're providing to the DB::transaction function, i'm not sure why it would be working on live local, but you must import the instance of the controller into the function to use it.
The best way to do that, to avoid confusion, would be to alias it too, and possibly pass it by reference so you're not copying it, like:
$this_var = $this;
DB::transaction(function() use ($user_id, &$this_var as $controller) {
$org = $this->create_org($user_id); //failing on this line with exception. btw $user is created fine
$controller->create_contact($org->id);
$controller->create_address($org->id);
});
I'm not entirely sure if the syntax is perfect, but the logic is sound.
So I'm using PHPUnit for testing. Trying to use a DataProvider with one of my tests.
/**
* Tests Events_Event->Events_Event()
* #dataProvider provider
*/
public function testEvents_Event($Name, $param, $time) {
//$this->assertInstanceOf("Events_Event", $this->Events_Event->Events_Event("test2", array()));
$this->assertTrue(true);
}
public static function provider()
{
return array(
array("test", array("Like a boss"), "Cheack the time"),
array("test2", array("Like a boss"), "9:00"),
array("test3", array("Time to go home"), "4:00"),
array("test3", array("Time to go home"), "4:00")
);
}
The results:
testEvents_Event with data set#0
testEvents_Event with data set#1
testEvents_Event with data set#2
testEvents_Event with data set#3: The test case was unexpectedly terminated
This happens on the last data set no matter how many there are and whether or not the last data set is valid of not. As you can see, we've simplified the test to a simple $this->assertTrue(true) and it's still giving us the error.
What do we need to do to get the Data Provider working?
In case it's important I'm working PHPUnit inside Zend Studio 9.0.3, I've checked for updates and it's telling me all is up to date.
I was going through
....
Time: 0 seconds, Memory: 12.75Mb
OK (4 tests, 0 assertions)
/**
* Tests Events_Event->Events_Event()
* #dataProvider provider
*/
public function testEvents_Event($Name, $param, $time)
{
}
public static function provider()
{
return array(
array("test", array("Like a boss"), "Cheack the time"),
array("test2", array("Like a boss"), "9:00"),
array("test3", array("Time to go home"), "4:00"),
array("test3", array("Time to go home"), "4:00")
);
}
how to you run tests? there do not have any other dependencies?
tests runing via any IDE?
PHPUnit instantiates the test case for each data provider method. Due to the magic of PHP you can get away with using static data provider methods, but they are called using an instance and thus should be non-static.
If your test case has a constructor, it must accept three parameters (see the source for PHPUnit_Framework_TestCase) and pass them to the parent constructor. One of them is the data from a provider for that particular test.
I doubt these are the problem, however. My money's on ZendStudio and how it parses the output from PHPUnit as Gordon suggested. When you run this test case from the command line, do you see the same issue?