Why having Issues when calling Model data into Controller to view? - codeigniter-4

I'm a beginner at Codigniter4. I'm just wondering "Am I missing something or my Code has some kind of problem?" Please check my code below, if anybody can help me to solve this issues I will be so glad full. I'm having the warning "Whoops! We seem to have hit a snag. Please try again later...".
Router.php
$routes->get('/country', 'CountryController::index');
CountryController.php
namespace App\Controllers;
use App\Models\CountryModel;
class CountryController extends BaseController{
public function index()
{
$country = new CountryModel();
$data['country'] = $country->findAll();
return view('country/index.php', $data);
}}
CountryModel.php
namespace App\Models;
use CodeIgniter\Model;
class CountryModel extends Model{
protected $table = 'country';
protected $primaryKey = 'id';
protected $allowedFields= [
'countryName',
'countryFlag'
];}
index.php
print_r($country);

Related

codeigniter 4 Undefined property: App\Controllers\Test::$load

I'm switching to Condeigniter 4.
Trying to do some basic stuff.
I can't load the model into controller function.
Controller Test.php:
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load->model('user_model');
}
}
Model User_model.php
<?
namespace App\Models;
use CodeIgniter\Model;
class User_model extends Model
{
public function get_user_info($user_id)
{
return 1;
}
}
I'm getting error: Undefined property: App\Controllers\Test::$load
What am I doing wrong?
Codeigniter 4 and 3 have different way to load model. In codeigniter 4 you can use
use CodeIgniter\Model\User_model;
$modelUser = new \App\Models\User_model();
or
$userModel = model(User_model::class);
or you can see in Codeignter 4 User Guide, Accessing Models
Model loading approach has been changed to Codeigniter 4. The way of loading the models that you use in your code has been replaced. Check out the library Models and information about loading the model can be found here Accessing Models.
Controller - test code
<?php namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$TestModel = new TestModel;
$this->data['test'] = $TestModel->test(2);
return view('App\Views\test',$this->data);
}
}
Model - Test code
<?php namespace App\Models;
use CodeIgniter\Model;
class TestModel extends Model
{
public function test($var = 1)
{
return $var;
}
}
The above sample code allowed us to reference the $test variable in the view, which will display the value 2.

Override BQL used for PXProjection on SOShipmentPlan

I have a need to override the Select statement being used for the SOShipmentPlan PXProjection/DAC, namely, removing the
And <INPlanType.isFixed, Equal<boolFalse>
condition.
I can override all of the CreateShipment() logic and bring in any other necessary routines into an SOShipmentEntry_Extension class, to the point where I finally can use my own version of a SOShipmentPlan class, but that all seems needlessly complex when all I want to do is override the select for the PXProjection attribute. Overriding CreateShipment() and supporting routines also seems like a quick way to get in trouble come time for upgrades.
So, is there an easy way to override the PXProjection's BQL, or am I stuck overriding all kinds of code?
UPDATE 1
Based on a link provided below (stackoverflow.com/a/41540659/7376238), I feel like I'm close. Here's the block of code I end up with:
namespace PX.Objects.SO
{
public class SOShipmentEntry_Extension : PXGraphExtension<SOShipmentEntry>
{
#region Event Handlers
#endregion
[Serializable]
[PXProjection(typeof(Select2<SOOrder,
InnerJoin<SOOrderType, On<SOOrder.FK.OrderType>,
InnerJoin<INItemPlan, On<INItemPlan.refNoteID, Equal<SOOrder.noteID>>,
InnerJoin<INPlanType, On<INItemPlan.FK.PlanType>>>>,
Where<INItemPlan.hold, Equal<boolFalse>,
And<INItemPlan.planQty, Greater<decimal0>,
And<INPlanType.isDemand, Equal<boolTrue>,
And<INPlanType.isForDate, Equal<boolTrue>,
And<Where<INItemPlan.fixedSource, IsNull,
Or<INItemPlan.fixedSource, NotEqual<INReplenishmentSource.transfer>>>>>>>>>))]
[PXSubstitute()]
public partial class SOShipmentPlanCst : SOShipmentPlan
{
int x = 0;
}
}
But it doesn't seem to work. Not sure of where I'm supposed to put the code. I've tried putting the class definition inside and outside of public class SOShipmentEntry_Extension : PXGraphExtension<SOShipmentEntry> class (currently inside the extension class as shown). No luck either way.
THIS ANSWER DIDN'T WORK
Fair warning... I have not done this to a PXProjection before, so you'll have to see if this works. The nature of extensions tends to allow overriding views by simply redefining them. I have not done this myself with a projection, but I suspect it will be similar. Give it a try and see if you get the desired results. All I can say about testing it is that "it compiled" when I added to my project and removed the INItemPLanType.isFixed condition.
public class SOShipmentEntry_Extension : PXGraphExtension<SOShipmentEntry>
{
[PXProjection(typeof(Select2<SOOrder,
InnerJoin<SOOrderType, On<SOOrder.FK.OrderType>,
InnerJoin<INItemPlan, On<INItemPlan.refNoteID, Equal<SOOrder.noteID>>,
InnerJoin<INPlanType, On<INItemPlan.FK.PlanType>>>>,
Where<INItemPlan.hold, Equal<boolFalse>,
And<INItemPlan.planQty, Greater<decimal0>,
And<INPlanType.isDemand, Equal<boolTrue>,
And<INPlanType.isForDate, Equal<boolTrue>,
And<Where<INItemPlan.fixedSource, IsNull, Or<INItemPlan.fixedSource, NotEqual<INReplenishmentSource.transfer>>>>>>>>>))]
public partial class SOShipmentPlan : IBqlTable { }
}

Is there any way to access FXML variables in classes other than Controller Class?

I'm curious that is there any way to call #FXML variables in not only Controller Class but also in another classes. Well actually i'm dealing with SVGPath nodes and trying to implement various kind graphs. However i dont wanna write the whole code in only Controller Class. I will appreciate if you can help and also give clear answers. So thanks anyway :)
EDIT: let me introduce a simple example about my issue.
Controller class code section;
public class RiskControllerClass implements Initializable {
#FXML private SVGPath NA_1; // Alaska
#FXML private SVGPath NA_2; // NorthWest_Ter
.
.
}
Territory class which refers to a vertex in a graph
public class Territory {
public Territory(SVGPath nodeSVG, int territoryID, int playerID){
this.playerID = playerID;
this.territoryID = territoryID;
this.nodeSVG = nodeSVG;
this.label = nodeSVG.getId();
this.adjacencyList = new LinkedList<>();
this.edgeSet = new LinkedList<>();
}
.
.
}
so i want to implement my graph another class than Controller class something like GameBoard
public class GameBoard {
// Want to call #FXML instance variables here
}
There is no other way than to access your controller and call methods from it. There is no magical way an #FXML annotation makes private variables globally available. #FXML is only used to mark certain fields for the FXMLLoader so he can access them via reflection in the instantiation process of your FXML - nothing else.
Refere to this questions on how to access the controller:
Accessing FXML controller class
JavaFX: How to get stage from controller during initialization?

AutoMapper in a class library

I create a class library to put my repositories, domain model and my DTO.
When a user call ClienteRepository.GetById(1) for exemple, it should get the Client domain model and transform into a ClientDTO to return this, example:
public class ClientRepository{
public ClientDTO GetById(int id){
var clientDto = Mapper.Map<Client, ClientDTO>(_db.Client.Find(id));
return clientDto;
}
}
the problem is that Mapper.Map doesn't work because I did not create the map (Mapper.CreateMap<Client, ClientDTO>()).
My question: How can I do this in a class library if I dont have global.asax to create it?
You don't need a Global.asax for Automapper.
It's just the better way to do mapping init for a web project.
Just put your init code in a static constructor
static MyStaticCtor()
{
//samples
//Mapper.CreateMap<AccountViewModel, Account>();
//Mapper.CreateMap<AccountSettingViewModel, AccountSetting>()
Mapper.AssertConfigurationIsValid();
}
or even, you can simply do this in the constructor of your Repository.
I solved my problem using https://github.com/davidebbo/WebActivator. Just create a new class and put this code:
[assembly: WebActivator.PostApplicationStartMethod(typeof (MapsInit), "Activate")]
namespace Database
{
public static class MapsInit
{
public static void Activate()
{
Mapper.CreateMap<ClienteDto, Cliente>();
Mapper.CreateMap<Cliente, ClienteDto>();
}
}
}

The requested view 1.php could not be found in kohana

I'm trying to learn the framework kohana.
I have defined a new controller under application/controller/classes. Which I named to hello.php:
class Controller_Hello extends Controller
{
public function action_say(){
$g = new View('firstv');
$g->render(TRUE);
}
}
?>
And I have this under application/views. Which I called firstv.php:
<h1>testing1</h1>
What's the mistake here. I'm using this guide:
http://pixelpeter.com/kohana/kohana101.pdf
I'm using the latest stable version 3.1.3.1. I have called the function by navigating to:
http://localhost/kohana/index.php/hello/say
Tried this using the same say function. And it worked. But this one doesn't use views.
$this->response->body('hello, world 2!');
Please help, thanks.
$this->response->body($g->render());
So your complete action method will be something like:
public function action_say()
{
$g = new View('firstv');
$this->response->body($g->render());
}
or:
public function action_say()
{
$g = new View('firstv');
$this->response->body($g);
}
or even:
public function action_say()
{
$this->response->body(new View('firstv'));
}

Resources