Search code examples
phpmysqlsqloxid

Performing an SQL query in oxid


I am fairly new to OXID.

I am trying to perform an SQL query by using OXID functions only.

The point is to check if $discount exists in oxVoucherSerie's table oxdiscount, and if it exists get the oxid from oxVoucherSerie that corresponds to that discount.

Sadly I cannot use plain MySQL and I am only allowed to use oxid functions. I have no idea how to do it.

    public function save_voucher($discount){
$o_voucherid         = oxNew("oxVoucherSerie");
$aWhere_discount['oxdiscount']    = $discount;
$sSql                       = $o_voucherid->buildSelectString($aWhere_discount);
$b_exists                   = $o_voucher->assignRecord($sSql);
}

This tells me if the discount exists. However, I have no idea to retrieve oxid from said discount.


Solution

  • we might need to clarify some wording and variables first, because i'm not really sure what you are trying to achieve.

    voucher - (also called "coupon") shop visitor can enter voucher/coupon code in basket to achieve a price reduction or a free product.
    Vouchers are generated in Shop Settings -> coupon series

    discount - general price reduction for some categories or articles, e.g. 10% for pet accessories.
    Discounts can be configured in Shop Settings -> Discounts


    okay, i updated my post.

    your code works well, i just changed it a little bit. First the code and then the explanation:

    $o_voucher = oxNew("oxVoucherSerie");
    $aWhere_discount['oxdiscount'] = 25;
    $sSql = $o_voucher->buildSelectString($aWhere_discount);
    $o_voucher->assignRecord($sSql);
    var_dump($o_voucher);
    

    I replaced $o_voucherid with $o_voucher (without id)
    $o_voucher = oxNew("oxVoucherSerie"); gives you an oxVoucherSeries Object
    $o_voucher->assignRecord($sSql); does not only tell you if voucher series with such discount exists, it also will load this voucher serie into $o_voucher , so you can access all the (not protedted) parameters of this voucher serie by using $o_voucher, e.g.:
    $o_voucher->getId() or $o_voucher->oxvoucherseries__oxid->value() for getting its oxID or just var_dump($o_voucher); to see all the objects properties

    By the way, if you are developing with OXID 4.9 you could use this module: https://marat.ws/direct-access-to-oxid-framework-functions/ for quick evaluating and debugging you code. just copy-paste the code from above into the textarea and hit the button