How to do patchEntity in cakephp - cakephp

Hi I'm making a webpage to get phone number from registerer and make random number as a pin number to save in DB.
and I want to show registerer to enter the random number.
So I made these code.
class RegistersController extends AppController{
public function index()
{
$random = rand(11111,99999);
$register = $this->Registers->newEntity($this->request->data);
if($this->Registers->save($register)) {
$this->Flash->success('The Phone number has been sent.');
$reg = $this->Registers->patchEntity($random);
$this->Registers->save($reg);
return $this->redirect(['action' => 'certnum']);
}
$this->set(compact('register'));
}
}
But somehow It makes fatal error which I have no idea what to do?
Do i have to the random number into array with certain id number or something?
Please help.
Thank you

Back to developer school: When asking for help and describing a problem always post the complete error message, any warning, notice, stack trace and other relevant debug output as well.
I guess the error message I don't know is related to the fact that you try you pass an invalid data type to patchEntity(). It expects an array, you are passing an integer, because that's what rand() generates. Put the integer in whatever array structure you expect and it should work.

Related

Handling elements which returns String value in two lines

I'm new to perfecto mobile device automation. I came across a name field which returns value in two lines in the application. Like below
School Name : ABCD
INTERNATIONAL
I found the locator which is Xpath as //*[text()="ABCD INTERNATIONAL"], this returns value in two lines which I confirmed when I ran the code and asserted the value. So basically the error is like below
expected [ABCD INTERNATIONAL] but found [ABCD
INTERNATIONAL]
My feature file is like
Feature: Verify the school Details
Scenario : Verify the school Name
Given User logs into Application
When User verifies the school name as "ABCD INTERNATIONAL"
Then user logs of
Step Defenition:
#When("User verifies the school name as {string}")
public void a_User_verifies_the_school_name_as (String Name) throws Exception {
String School_Name = utility.getText(appiumDriver, "//*[text()="ABCD INTERNATIONAL"]);
Assert.assertEquals(School_Name, Name);
}
Your help in sorting out this will mean a lot.
I think you have to modify your actual and expected string by using replaceAll() method.
Like replaceAll("\n",""); and replaceAll(" ",""). then compare both in assertion.
If it is not working then you have to use pattern matching by regular expression.

Getting rates from an array - json

I am trying to get the rates from this website.
So I connect with website = Faraday.get('https://bitpay.com/api/rates')).status == 200 and then try to parse this.
A segment of the response I get is:
#<Faraday::Response:0x007fcf1ce25688
#env=
#<struct Faraday::Env
method=:get,
body=
"[{\"code\":\"BTC\",\"name\":\"Bitcoin\",\"rate\":1}, {\"code\":\"USD\",\"name\":\"US Dollar\",\"rate\":586.66},{\"code\":\"EUR\",\"name\":\"Eurozone Euro\",\"rate\":528.991322},{\"code\":\"GBP\",\"name\":\"Pound Sterling\",\"rate\":449.441986},{\"code\":\"JPY\",\"name\":\"Japanese Yen\",\"rate\":59907.95922},{\"code\":\"CAD\",\"name\"
When I do a website.body I get a String class of all these values found on that website. I want to parse them though (JSON?) so that I can get each rate as a float.
I tried something JSON.parse(website.body)["GBP"]["rate"].to_f but yet again it cannot work in a string.
The return I get is TypeError: no implicit conversion of String into Integer
I was having a similar (but not the same) format from a different rates website and this is how I was handling it. Do I need to change its format first or is there a different way around it?
You're trying to access to the parsed JSON with the key "GBP" but you have an array. It's like if you did
a = [1,2,3,4,5]
a['foo']
Try out
currencies = JSON.parse(website.body)
currencies.each { |currency| puts currency['rate'] }
and change it like you need

AS3: hitTestObject turning up null for no apparent reason

I'm currently coding a game, and in one part of the code, it's checking to see if the player is close enough to a monster in mArray for its health bar to appear. I use hitTestObject for this with var i incrementing to go through the list. However, I will randomly get this error message:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
After seeing this for the first time, I put the test all under the conditional if (mArray.length > 0) to make sure the Array() was filled before even attempting the test... but it still occurs at random times. I even used trace(i) to see if it happened on a certain part of the list, but it's completely random each debug session! I know the player portion can't be null since it is on the screen at all times during the hitTest, so I'm at a loss for words right now.
The portion of code in question (nothing else is related to this except for the use of mArray[i] in different places):
for (var i:int = 0; i < mArray.length; i++) {
if (mArray.length > 0) {
trace(i); //my attempt to see if it occurred at a specific incrementation of i
if (player.hitTestObject(mArray[i])) {
mArray[i].healthBar.visible = true;
} else {
mArray[i].healthBar.visible = false;
}
}
}
Again, it works perfectly for everything all of the time except for random cases it gives me the TypeError. Is this just a hiccup in my code, is there some obvious problem that I'm overlooking, or does the code just like to throwback to 2007?
EDIT: The debug session ALWAYS points to the line with if (player.hitTestObject(mArray[i])).
It looks like for some i the mArray[i] has a null value. Either make sure this won't happen when populating the array or just add extra check in hitTestObject line like this:
if (mArray[i] && player.hitTestObject(mArray[i])) { ... }
I don't know your game's architecture but maybe it would be more convenient to move the hitTestObject test into monster class instead of checking it externally. Typical approach is to have each entity to have some kind of "update" method that is executed once in every game loop.

return of getText() cannot be compared to a string

I am using a following code to compare 2 strings in one of protractor/jasmine test cases.
emailnotsentmessage.getText().then(function(text) {
expect(text).toBe('has not received notification about recent changes to the meeting.');
});
where emailnotsentmessage contains following text
[ 'has not received notification about recent changes to the meeting.' ]
for some reason , the string comparison fails . those two strings contains absolute same content. i checked it several times . am i missing something here ?. the emailnotsentmessage is a content of a <span> .
error trace
1) Get to the existing meeting by navigating to the edit meeting page should display the same value which was entered du
ring create meeting when go into edit meeting
Message:
Expected [ 'has not received notification about recent changes to the meeting.' ] to equal 'has not received notific
ation about recent changes to the meeting.'.
Stack:
Error: Failed expectation
Looks like your emailnotsentmessage is array and not a string. What if you try expect(text).toBe(['has not received notification about recent changes to the meeting.']); or maybe emailnotsentmessage[0]
found a fix and the reason behind this issue
i was earlier using var emailnotsentmessage = element.all(by.css('css path')); to get the element which caused the problem . It returns the string in an array format . instead i used the element(by.css('css path')); and it returns the expected string value .

Select random item from an array with certain probabilities and add it to the stage

Its quite a big task but ill try to explain.
I have an array with a list of 200 strings and I want to be able to randomly select one and add it to the stage using code. I have movieclips exported for actionscript with the same class name as the strings in the array. Also, if it is possible, would I be able to select the strings with predictability such as the first has a 0.7 chance the second a 0.1 etc. Here is what i have currently
var nameList:Array=["Jimmy","Bob","Fred"]
var instance:DisplayObject = createRandom(nameList);
addChild(instance);
function createRandom(typeArray:Array):*
{
// Select random String from typeArray.
var selection:String = typeArray[ int(Math.random() * typeArray.length) ];
// Create instance of relevant class.
var Type:Class = getDefinitionByName(selection) as Class;
// Return created instance.
return new Type();
}
All this throws me this error
ReferenceError: Error #1065: Variable [class Jimmy] is not defined.
Ive searched for other threads similar but none combine the three specific tasks of randomisation, predictability and addChild().
I think that you've got two problems: a language problem and a logic problem. In the .fla connected to your code above, in the Library find each symbol representing a name and write into the 'AS linkage' column for that symbol the associated name -- e.g., 'Bob,' 'Fred' -- just the name, no punctuation.
Now getDefinitionByName() will find your 'Class'
If you put a different graphic into each MovieClip -- say, a piece of fruit or a picture of Bob,Jim, Fred -- and run your program you'll get a random something on stage each time.
That should solve your language problem. But the logic problem is a little harder, no?
That's why I pointed you to Mr. Kelly's solution (the first one, which for me is easier to grasp).

Resources