Create class if class not exist with PhpOrient - orientdb

Is it possible to create a class only if the class doesn't exists yet,
in PhpOrient for OrientDB 2.1.13?
So something like this:
$client->command('create class Thing extends V if class Thing != exist');

That behaviour is built in. If you try to create a class that already exists OrientDB will return an error (if you are doing it through the console). If you are using PhpOrient, it will throw an exception.
If the class does exist it will return the number of total classes in the db after creation
So just check the response, but catch the exception as well.:
$response=false;
try {
$response = $client->command('create class Thing extends V);
} catch (Exception $e) {
print "Error: " . $e->getMessage() ."\n";
//class was most likely already created
}
//check $response
if ($response) {
//class created scuccessfully
print"Number of claasses $response \n";
}
You can of course set flags as you need them inside the blocks so you can do whatever you need depending on the outcome.
Hope this helps

Related

How to define a class that is exactly the same as another class in Dart/Flutter

I'm defining some custom Exceptions in Dart.
I want in my logic to check the type of exception and base my processing on that, so I want to create distinct classes for each, for example like this :
class FailedToLoadCriticalDataException implements Exception { } // app cannot continue
class FailedToLoadNonCriticalDataException implements Exception { } // app can continue
However I also want to pass 2 parameters when I create these types of exceptions, the type of API call, and the API url, and the definition for that would look like this :
class UrlCallFailedException implements Exception {
String _dataTypeName;
String _urlEndpoint;
UrlCallFailedException([this._dataTypeName, this._urlEndpoint]);
#override
String toString() {
return "(${this.runtimeType.toString()}) Failed to fetch $_dataTypeName ($_urlEndpoint)";
}
}
Now what I want to do is (replace the initial definitions I made earlier and re)define my FailedToLoadCriticalDataException and FailedToLoadNonCriticalDataException classes so that they are exactly the code that is in the UrlCallFailedException class.
Is there any way to simply say something like class FailedToLoadCriticalDataException **is** UrlCallFailedException; and not need to duplicate the code that defines UrlCallFailedException ?
class FailedToLoadCriticalDataException implements UrlCallFailedException{ } is wrong because it is "Missing concrete implementations of 'getter UrlCallFailedException._dataTypeName',.."
class FailedToLoadCriticalDataException extends UrlCallFailedException{ } is wrong because when I got to throw FailedToLoadNonCriticalDataException("Foo", url); it's expectation is that there are no params ("Too many positional arguments: 0 expected, but 2 found.").
Is there a way to create multiple classes that behave exactly the same as another type and differ only in their class, without duplicating all the code ?
I've come up with this as a decent compromise :
class FailedToLoadCriticalDataException extends UrlCallFailedException {
FailedToLoadCriticalDataException([dataTypeName, urlEndpoint]) {
super._dataTypeName = dataTypeName;
super._urlEndpoint = urlEndpoint;
}
}
class FailedToLoadNonCriticalDataException extends UrlCallFailedException {
FailedToLoadNonCriticalDataException([dataTypeName, urlEndpoint]) {
super._dataTypeName = dataTypeName;
super._urlEndpoint = urlEndpoint;
}
}
Some, but minimal, code duplication, and I can now call throw FailedToLoadNonCriticalDataException("Foo", url); in my code later.

filter records in popup list view if assigned user is login SUITECRM

I want to filter records so that the assigned user can only see the records that are assigned to him from the popup list view.
The reason why I'm not doing this in the roles management is because if I assigned a user to a client record then other users that have the same role wouldn't able to see it so I've set the role->list tab to "all" and added custom code in list view that only the login user can see their own records.
Here's what I've done.
<?php
require_once('include/MVC/View/views/view.popup.php');
class AccountsViewPopup extends ViewPopup
{
public function display()
{
parent::display(); // TODO: Change the autogenerated stub
require_once 'modules/ACLRoles/ACLRole.php';
$ACLRole = new ACLRole();
$roles = $ACLRole->getUserRoles($GLOBALS['current_user']->id);
if (in_array('User1', $roles)) {
global $db, $current_user;
$this->where .= " AND accounts.assigned_user_id = '$current_user->id' AND deleted=0 ";
}
}
}
But i get this error:
Undefined property: AccountsViewPopup::$where
For list view only: custom/modules/MODULE_NAME/views/view.list.php
and following is the helping code:
require_once('include/MVC/View/views/view.list.php');
class MODULE_NAMEViewList extends ViewList {
function listViewProcess() {
global $current_user;
$this->params['custom_where'] = ' AND module_name.name = "test" ';
parent::listViewProcess();
}
}
For list and popup view(both):
You need to change the logic inside create_new_list_query function which actually prepares a query. Some modules have override it a bean level(e.g. see modules/Leads/Lead.php).
If you want to override it in upgrade safe manner then create a file in custom directory e.g: custom/modules/Leads/Lead.php, then extend it from the core bean class like following:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Leads/Lead.php');
class CustomLead extends Lead {
function create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false,$parentbean=null, $singleSelect = false, $ifListForExport = false)
{
// Code from create_new_list_query in and then modify it accordingly.
}
}
Register new bean class in this location: custom/Extension/application/Ext/Include/custom_leads_class.php and registration code will look like following:
<?php
$objectList['Leads'] = 'Lead';
$beanList['Leads'] = 'CustomLead';
$beanFiles['CustomLead'] = 'custom/modules/Leads/Lead.php';
?>
I know this has been answered, but decided to post my solution anyway. I had almost the same problem some time ago (7.10.7).
PopupView has method getCustomWhereClause() which you can implement in your custom view.
It has to return containing string with the conditions.
Example:
custom/modules/Meetings/views/view.popup.php
/*class declaration and other stuff*/
protected function getCustomWhereClause()
{
global $current_user;
return " ( {$this->bean->table_name}.assigned_user_id='{$current_user->id}') ";
}
Remember to leave at least one space at the start and the end because SuiteCRM actually forgets to add it and it may result in broken query (but it's fairly easy to find in logs).

J2EE reflection class

I am using reflection to call the constructor of a class called Child which requires the package: org.apache.commons.lang3.ArrayUtils
I've added it to the class path and have been using it else where just fine, but reflection does not seem to see my class path.
Error:
java.lang.NoClassDefFoundError: org/apache/commons/lang3/ArrayUtils
I've searched and seen lots of writing about class loader but I still do not understand this. Could someone please explain the problem and how to solve it as clearly as possible?
//main class:
try {
Class<?> claz = Class.forName(claz_str);
Parent p = (Parent) claz.getDeclaredConstructor(String.class, String[].class, String[].class).newInstance(elmName, nodes_arr, params_arr);
} catch (Exception e) {
throw new Exception(e.getCause());
}
// Child.class
import org.apache.commons.lang3.ArrayUtils;
public class Child extends Parent{
public Child(){
if(!ArrayUtils.contains(NUM_OF_PARAMS, 2)){ // <<====== run time error
System.out.println("stuff")
}
}
}
Your code seems OK. Are you providing full qualify name of your class as claz_str.
It should be
claz_str = "com.package.Class";
Hope solve your problem.

Undefined variable: mysqli - PHP OOP

How can I implement mysqli in an extended class?
I am uploading an image and storing it in a MySQL database, but I get this error:
Notice: Undefined variable: mysqli in ...ecc/ecc/ on line 33
Fatal error: Call to a member function query() on a non-object in ...ecc/ecc/ on line 33
Here is my test code:
<?php
interface ICheckImage {
public function checkImage();
public function sendImage();
}
abstract class ACheckImage implements ICheckImage {
public $image;
private $mysqli;
public function _construct(){
$this->image = $_POST['image'];
$this->mysqli = new mysqli('localhost','test','test','test');
}
}
class Check extends ACheckImage {
public function checkImage() {
if($this->image > 102400) {
echo "File troppo grande";
}
}
public function sendImage() {
//This is the line 33 give me the error
if ($mysqli->query("INSERT INTO images (image) VALUES ('$this->image')")) {
echo "Upload avvenuto &nbsp";
} else {
echo "Errore &nbsp" . $mysqli->error;
}
}
}
$form = new Check();
$form->checkImage();
$form->sendImage();
?>
There are some errors in your code.
The $mysqli member is private inside the abstract class. It will not be inherited by the Check class, so it does not exist there. Make it protected.
Access to the members of a class always needs $this-> in front, specifically $this->mysqli in this instance.
The constructor function must be named __construct with two underscores in front.
The image check looks wrong. $_POST['image'] does contain something that you expect to store in the database, but you also compare it with an integer value and seem to echo an error message if it is bigger. While the data handling will work, e.g. you can compare a string from POST data with an integer, it looks like you want something else.

how to dump generated query

i have an update query that i am getting error
Message: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
i would like to have my query display so that i could debug it. any ideas how to do this
EDIT::
on my page.php model i have and Page model extends Zend_Db_Table_Abstract
public static function updatePage($data, $id)
{
$row = new self();
$row->update($data, "page_id = ?".$id);
}
If you use "Zend_Db_Select":
<?php
Zend_Debug::dump($select->__toString()); // nice formatted debug
echo $select; // altough ok !