I'm trying to run a full text search against some data that is stored in mongoDb using Lithium.
Here is how I am trying to do it in my controller:
$mongodb = Connections::get('default')->connection;
$results = Page::connection()->connection->command(array("text" => "Page", 'search' => "term" ));
I've also tried:
$results = Page::connection()->connection->command(array("text" => "Page", 'search' => "term" ));
However, both of these return: Fatal error: Call to a member function command() on a non-object
What am I doing wrong?
EDIT:
I should add that a simple query on Page is working just fine. For instance:
$results = Page::find('all');
Does return an array with all of the documents in the pages collection like I would expect it to.
UPDATE 2:
I was running all of this from WAMP server. I tried today running it from a linux server, but still got the same exact error. I am really stumped by this and could use some help. Anyone have any ideas?
here is the Page model as it sits now:
<?php
namespace app\models;
use lithium\data\Connections; //added during debugging
use lithium\data\source\MongoDb; //added during debuging
class Page extends \lithium\data\Model {
}
?>
Here is my connection:
Connections::add('default', array(
'type' => 'MongoDb',
'host' => '192.168.48.128',
'database' => 'my_collection'
));
I'm doing it this way:
$plugins = Plugins::connection()->connection->command([
'text' => 'plugins',
'search' => $this->request->query['q']
]);
return compact('plugins');
so I'd recommend checking your configuration - is your model returning other data normally? Is the connection configuration correct?
Got help to figure it out... posting here for others to reference.
proper way of calling it is:
$conn = Model::connection();
$db = $conn->selectDB('db');
$result = $db->command(array(...
Works perfectly when done this way.
Related
Actually , I did the JSON calling from the PHP file named as student_detail.php
localhost/demo/student_details.php
I need to call the JSON file inside the student_details.php file. like
localhost/demo/student_details/get_Students_Details
by calling this way it shows an error like object not found. Here my code
<?php
include('database.php');
class student_details
{
public function get_Students_Data()
{
$query = "select * from student_table";
$result = mysql_query($query);
$student_data = array();
while($row = mysql_fetch_assoc($result))
{
$student_data[] = array(
'Student Id' => $row["student_id"],
'Register No' => $row["student_register_no"],
'Roll No' => $row["student_roll_no"],
'Name' => $row["student_name"],
'Date Of Birth' => $row["student_DOB"],
'Gender' => $row["student_gender"],
'Nationality' => $row["student_nationality"],
'City' => $row["student_city"],
'Pincode' => $row["student_pincode"]
);
};
echo json_encode($student_data,JSON_PRETTY_PRINT);
}
}
?>
How do I resolve it.?
You are using "clean urls", which pure PHP doesnt support.
Its unclear if you are using a framework which does support that or not.
Your code, as is, should work if you are using a framework with clean urls.
Please provide some more informations for further help.
If you do NOT use a framework, all you have to do is create a new file, which will be called if you want to request a JSON response. Then you create a new object of your student_details class and call the method get_Student_Data().
i.e:
<?php
include('classes/student_details.php');
$student = new student_details;
$student->get_Student_Data();
Unrelated, but still important: do NOT use mysql_* functions, since they are deprecated in PHP 5.5 and removed from PHP 7. Use mysqli_* or PDO instead.
Given that I have a WHMCS addon that I call 'my_addon'. I created the main addon file 'my_addon.php' which does contain nothing than:
<?php
function my_addon_clientarea($vars) {
$client = null;
return array(
'pagetitle' => 'My Addon',
'breadcrumb' => array('index.php?m=my_addon'=>'My Addon'),
'templatefile' => 'views/myaddon_view',
'vars' => array(
'client' => $client
)
);
}
This does basically work. It does give me my template file, everything is passed through. My question is: How do I get the currently logged in client from within that function?
I didn't find any API method and I can't see any constant which does hold this information.
There must be a way to get the current client within the clientarea? Thanks for your help!
For those who do come after me and have the same problem: it's easy to solve. Turned out, that I just had to think it through... I found the client id to be available in the $_SESSION-variable.
So, if you are looking for the client's id:
<?php
function my_addon_clientarea($vars) {
$clientid = $_SESSION['uid'];
// And so on...
}
The official way to get current user information is:
$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
You can find more information here
I don't know if this is possible or if it's a complete madness but I'm trying to execute a PHP method from AJAX call using OctoberCMS Ajax Framework(I assume that this uses jQuery behind it) and is not working because I never get redirect to PayPal site. The PHP code I'm trying to get working is this one:
protected function onExecutePurchaseMethod()
{
Omnipay::gateway('PayPal_Express');
$params = [
'username' => $this->username,
'password' => $this->password,
'signature' => $this->signature,
'testMode' => $this->sandboxMode,
'amount' => Session::get('amountToReload'),
'cancelUrl' => url( 'payment/step4', "", $secure = null ),
'returnUrl' => url( 'payment/step2', "", $secure = null ),
'currency' => 'USD'
];
$response = Omnipay::purchase($params)->send();
if ($response->isSuccessful()) {
var_dump($response);
} else {
var_dump($response->getMessage());
}
}
What is happening since none redirect to PayPal is executed and page is getting stuck many times forcing me to close the browser and reopen again, no method is executed and no visible errors. It's possible to do what I'm trying to do? Is not a madness? If it's possible where is my error?
As extra info I'm using Barryvdh Laravel-omnipay package for handle Omnipay from within Laravel.
After looking briefly through the documentation, my best guess is that you're missing a required field for the purchase() method. I believe you need a card parameter (even if it's an invalid one) to get it to process.
I would like to connect to a second database with Yii at runtime. The database name would come from a database table after the user to login.
I saw in a tutorial I should do this:
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
));
Yii::app()->setComponent('db2',$db2);
But in my controler when I access Yii::app()->db2 get the error:
Property "CWebApplication.db2" is not defined
What am I doing wrong?
The following works for me:
Yii::app()->mongodb->setActive(false);
Yii::app()->mongodb->setServer('mongodb://localhost:27017');
Yii::app()->mongodb->setDb('db1');
Yii::app()->mongodb->setActive(true);
UPDATED: Try, instead instance, pass configurations:
Yii::app()->setComponent( 'db2', array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
)
);
Or, you may create special index on params in configurations, such as:
...
'params' => array(
'db2' => null,
),
And the use Yii::app()->params['db2'] = $db2
From this comment:
My problem is not with the creation of the component. Soon after
creating if I access Yii::app()->db2 its works, but when I try to
access via another model or controller I get the error
I think you are setting this component only once somewhere, and then making subsequent requests to different controllers.
You need to put the code, somewhere it is being called EVERYTIME, on every Request. thats how PHP works, there is no "global application state"
by default Yii comes with protected/components/controller.php has base controller for the rest of the app.
my suggestion would be to put your code on the init() method of that controller, so that it always gets called.
You mentioned the database name comes from a table once the user logs in, so you need to save that value in the session, in other to be able to access it in the other requests:
<?php
// After login in
Yii::app()->user->setState('db_name', $db_name);
// in protected/components/controller.php
public function init()
{
if (!Yii::app()->user->isGuest) {
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => Yii::app()->user->getState('db_name')
));
Yii::app()->setComponent('db2',$db2);
}
}
Hope it helps, I am assuming many things here :)
I have a controller which I use for a login form. In the view, I have a {error} variable which I want to fill in by using the parser lib, when there is an error. I have a function index() in my controller, controlled by array $init which sets some base variables and the error message to '':
function index()
{
$init = array(
'base_url' => base_url(),
'title' => 'Login',
'error' => ''
);
$this->parser->parse('include/header', $init);
$this->parser->parse('login/index', $init);
$this->parser->parse('include/footer', $init);
}
At the end of my login script, I have the following:
if { // query successful }
else
{
$init['error'] = "fail";
$this->parser->parse('login/index', $init);
}
Now, of course this doesn't work. First of all, it only loads the index view, without header and footer, and it fails at setting the original $init['error'] to (in this case) "fail". I was trying to just call $this->index() with perhaps the array as argument, but I can't seem to figure out how I can pass a new $init['error'] which overrides the original one. Actually, while typing this, it seems to impossible to do what I want to do, as the original value will always override anything new.. since I declare it as nothing ('').
So, is there a way to get my error message in there, or not? And if so, how. If not, how would I go about getting my error message in the right spot? (my view: {error}. I've tried stuff with 'global' to bypass the variable scope but alas, this failed. Thanks a lot in advance.
$init musst be modified before generating your view.
To load your header and footer you can include the following command and the footer's equivalent into your view.
<?php $this->load->view('_header'); ?>
to display errors, you can as well use validation_errors()
if you are using the codeigniter form validation.
if you are using the datamapper orm for codeigniter you can write model validations, and if a query fails due to validation rule violation, you get a proper error message in the ->error property of your model.
Code for your model:
var $validation = array(
'user_name' => array(
'rules' => array('required', 'max_length' => 120),
'label' => 'Name'
)
);
You might try this:
function index() {
$init = array(
'base_url' => base_url(),
'title' => 'Login',
'error' => ''
);
$string = $this->parser->parse('include/header', $init, TRUE);
$string .= $this->parser->parse('login/index', $init, TRUE);
$string .= $this->parser->parse('include/footer', $init, TRUE);
$this->parser->parse_string(string);
}
In parse()you can pass TRUE (boolean) to the third parameter, when you want data returned instead of being sent (immediately) to the output class. By the other hand, the method parse_string works exactly like `parse(), only accepts a string as the first parameter in place of a view file, thus it works in conjunction.